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 / C++ / MFC
  4. Binary Data Manipulation

Binary Data Manipulation

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 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.
  • H Offline
    H Offline
    humps
    wrote on last edited by
    #1

    Hi All, I have an arbitrarily large block of binary data (a sequence of bytes). I want to write a function: int FindBit(BYTE* fisrtByte, int numBytes) that can be passed a pointer to the start of the data (firstByte), and the number of bytes of data (numBytes), and find the first occurrence of a "1" bit. e.g., if I pass a pointer to the following 3 bytes of binary data: 00001101 10110101 10110101 I need the function to return 5 - because the first non-zero bit is in position 5. Has anyone ever done something similar? Is there an efficient way to do this ? ;) Thanks, Neil Humphreys.

    G G 3 Replies Last reply
    0
    • H humps

      Hi All, I have an arbitrarily large block of binary data (a sequence of bytes). I want to write a function: int FindBit(BYTE* fisrtByte, int numBytes) that can be passed a pointer to the start of the data (firstByte), and the number of bytes of data (numBytes), and find the first occurrence of a "1" bit. e.g., if I pass a pointer to the following 3 bytes of binary data: 00001101 10110101 10110101 I need the function to return 5 - because the first non-zero bit is in position 5. Has anyone ever done something similar? Is there an efficient way to do this ? ;) Thanks, Neil Humphreys.

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

      Hi, This would work. int FindBit(BYTE* firstByte, int numBytes){ int pos = 0; int index = 0; BYTE current_bit = 1 << 8; while(index != numBytes){ if((firstByte[index] & current_bit) != 0) break; pos++; current_bit = current_bit >> 1; if(current_bit == 0){ current_bit = 1 << 8; index++; } } return pos; } NOTE: With your example, 00001101 10110101 10110101, this function would return a 4, because the count starts from zero. So if you want the count to start at 1, you'd have to add a 1 to the return value.

      1 Reply Last reply
      0
      • H humps

        Hi All, I have an arbitrarily large block of binary data (a sequence of bytes). I want to write a function: int FindBit(BYTE* fisrtByte, int numBytes) that can be passed a pointer to the start of the data (firstByte), and the number of bytes of data (numBytes), and find the first occurrence of a "1" bit. e.g., if I pass a pointer to the following 3 bytes of binary data: 00001101 10110101 10110101 I need the function to return 5 - because the first non-zero bit is in position 5. Has anyone ever done something similar? Is there an efficient way to do this ? ;) Thanks, Neil Humphreys.

        G Offline
        G Offline
        gdocherty
        wrote on last edited by
        #3

        Sorry, I've corrected some things. int FindBit(BYTE* firstByte, int numBytes){ int pos = 0; int index = 0; BYTE current_bit = 1 << 7; while(index != numBytes){ if((firstByte[index] & current_bit) != 0) break; pos++; current_bit = current_bit >> 1; if(current_bit == 0){ current_bit = 1 << 7; index++; } } return pos; } NOTE: With your example, 00001101 10110101 10110101, this function would return a 4, because the count starts from zero. So if you want the count to start at 1, you'd have to add a 1 to the return value.

        1 Reply Last reply
        0
        • H humps

          Hi All, I have an arbitrarily large block of binary data (a sequence of bytes). I want to write a function: int FindBit(BYTE* fisrtByte, int numBytes) that can be passed a pointer to the start of the data (firstByte), and the number of bytes of data (numBytes), and find the first occurrence of a "1" bit. e.g., if I pass a pointer to the following 3 bytes of binary data: 00001101 10110101 10110101 I need the function to return 5 - because the first non-zero bit is in position 5. Has anyone ever done something similar? Is there an efficient way to do this ? ;) Thanks, Neil Humphreys.

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          Hmmm. Just for fun, here's a slightly different approach:

          int FindBit(BYTE *firstByte,int numBytes)
          {
          int result = -1;
          // find index of first non-zero byte
          int index = 0;
          while ((index < numBytes) &&
          (firstByte[index] == 0)) {
          index++;
          }
          // find index of bit in non-zero byte
          if (index < numBytes) {
          result = index * 8;
          BYTE mask = 0x01;
          while ((firstByte[index] & mask) == 0) {
          mask <<= 1;
          result++;
          }
          }
          return result;
          }

          My version of FindBit returns -1 in case there are no 1 bits. Also note that I'm assuming 'Intel' byte/bit ordering.


          Software Zen: delete this;

          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