Binary Data Manipulation
-
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.
-
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.
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.
-
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.
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.
-
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.
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;