Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. bit operations question for binary>decimal

bit operations question for binary>decimal

Scheduled Pinned Locked Moved C#
questiondatabasealgorithmstutorialannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmatteson
    wrote on last edited by
    #1

    i just started a socket application that recieves UDP data. The data that i am recieving is a netflow packet from cisco devices. to learn how to do this i have been searching the forums and looking at other people's code for reference. some code that i came across was the following. pretty much what you do is, passed a byte[2048] to the procedure ToUInt and specify the starting point to get the datafield in the byte and the length from that starting position to get. the first field in the packet is the version field which starts at position 0 and is 16 bits long. so you would end up passing to the following code the byte[2048] of your data buffer, 0 for your offset and 16 for your length. i understand mostly all of the code except that part there the Shift Right occurs and the ANDing against 0x0001. b is 10, and then after the bitwise operations is it 5. which is the correct version number for the netflow packet that my application recieved. public static uint ToUInt(byte[] datagram, int offset, int length) { uint total = 0; int byte_index; int bit_offset; int bit; byte b; for ( int i = 0; i < length; i++ ) { bit_offset = (offset+i) % 8; byte_index = (offset+i-bit_offset) / 8; b = datagram[byte_index]; bit = (int)(b >> (7 - bit_offset)); bit = bit & 0x0001; if ( bit > 0 ) { total += (uint)Math.Pow(2,length-i-1); } } return total; } i modified the code to print to a console the values of each variable during each iteration of the loop. here are the values. i bit_offset byte_index b bit total 0 0 0 0 0 0 1 1 0 0 0 0 2 2 0 0 0 0 3 3 0 0 0 0 4 4 0 0 0 0 5 5 0 0 0 0 6 6 0 0 0 0 7 7 0 0 0 0 8 0 1 5 0 0 9 1 1 5 0 0 10 2 1 5 0 0 11 3 1 5 0 0 12 4 1 5 0 0 13 5 1 5 1 4 14 6 1 5 0 4 15 7 1 5 0 5 i relize that the byte_index is just a counter for how many bits you have iterated through. once you went through 8 bits, incriment the byte index by 1. since the length is 16, that is two bytes and that would account for 0, and 1. i is just the counter set to the length of 16. i don't understand the B, bit, and total values. could someone explain the logic behind this to me?

    G 1 Reply Last reply
    0
    • M mmatteson

      i just started a socket application that recieves UDP data. The data that i am recieving is a netflow packet from cisco devices. to learn how to do this i have been searching the forums and looking at other people's code for reference. some code that i came across was the following. pretty much what you do is, passed a byte[2048] to the procedure ToUInt and specify the starting point to get the datafield in the byte and the length from that starting position to get. the first field in the packet is the version field which starts at position 0 and is 16 bits long. so you would end up passing to the following code the byte[2048] of your data buffer, 0 for your offset and 16 for your length. i understand mostly all of the code except that part there the Shift Right occurs and the ANDing against 0x0001. b is 10, and then after the bitwise operations is it 5. which is the correct version number for the netflow packet that my application recieved. public static uint ToUInt(byte[] datagram, int offset, int length) { uint total = 0; int byte_index; int bit_offset; int bit; byte b; for ( int i = 0; i < length; i++ ) { bit_offset = (offset+i) % 8; byte_index = (offset+i-bit_offset) / 8; b = datagram[byte_index]; bit = (int)(b >> (7 - bit_offset)); bit = bit & 0x0001; if ( bit > 0 ) { total += (uint)Math.Pow(2,length-i-1); } } return total; } i modified the code to print to a console the values of each variable during each iteration of the loop. here are the values. i bit_offset byte_index b bit total 0 0 0 0 0 0 1 1 0 0 0 0 2 2 0 0 0 0 3 3 0 0 0 0 4 4 0 0 0 0 5 5 0 0 0 0 6 6 0 0 0 0 7 7 0 0 0 0 8 0 1 5 0 0 9 1 1 5 0 0 10 2 1 5 0 0 11 3 1 5 0 0 12 4 1 5 0 0 13 5 1 5 1 4 14 6 1 5 0 4 15 7 1 5 0 5 i relize that the byte_index is just a counter for how many bits you have iterated through. once you went through 8 bits, incriment the byte index by 1. since the length is 16, that is two bytes and that would account for 0, and 1. i is just the counter set to the length of 16. i don't understand the B, bit, and total values. could someone explain the logic behind this to me?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I have added comments to part of the code: // calculate what bit to get in the byte bit_offset = (offset+i) % 8; // calculate what byte to get byte_index = (offset+i-bit_offset) / 8; // get the byte b = datagram[byte_index]; // shift the byte so the desired bit is at position zero bit = (int)(b >> (7 - bit_offset)); // clear all bits except the one at position zero bit = bit & 0x0001; // calculate the value of the bit and add to the total total += (uint)Math.Pow(2,length-i-1);

      --- b { font-weight: normal; }

      M 1 Reply Last reply
      0
      • G Guffa

        I have added comments to part of the code: // calculate what bit to get in the byte bit_offset = (offset+i) % 8; // calculate what byte to get byte_index = (offset+i-bit_offset) / 8; // get the byte b = datagram[byte_index]; // shift the byte so the desired bit is at position zero bit = (int)(b >> (7 - bit_offset)); // clear all bits except the one at position zero bit = bit & 0x0001; // calculate the value of the bit and add to the total total += (uint)Math.Pow(2,length-i-1);

        --- b { font-weight: normal; }

        M Offline
        M Offline
        mmatteson
        wrote on last edited by
        #3

        hey thanks a lot. this is helpful

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups