Parse a binary number
-
Hi all, I was wondering if there was a way to parse a binary number that I receive over a TCP port, into either 4 bit chunks or into hex. Thank you. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
DWORD dwSome32BitNumber;
WORD wFirstChunk = dwSome32BitNumber & 0x0000000f;
WORD wSecondChunk = (dwSome32BitNumber >> 8) & 0x0000000f;
WORD wThirdChunk = (dwSome32BitNumber >> 16) & 0x0000000f;
WORD wFourthChunk = (dwSome32BitNumber >> 32) & 0x0000000f;
...Does that make sense? Or you could dress it up a bit with:
WORD wChunks[8];
for (int nIndex = 0; nIndex< 8; nIndex++)
{
wChunks[nIndex] = dwSome32BitNumber & 0x0000000f;
dwSome32BitNumber >>= 8;
}
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
Hi all, I was wondering if there was a way to parse a binary number that I receive over a TCP port, into either 4 bit chunks or into hex. Thank you. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
DWORD dwSome32BitNumber;
WORD wFirstChunk = dwSome32BitNumber & 0x0000000f;
WORD wSecondChunk = (dwSome32BitNumber >> 8) & 0x0000000f;
WORD wThirdChunk = (dwSome32BitNumber >> 16) & 0x0000000f;
WORD wFourthChunk = (dwSome32BitNumber >> 32) & 0x0000000f;
...Does that make sense? Or you could dress it up a bit with:
WORD wChunks[8];
for (int nIndex = 0; nIndex< 8; nIndex++)
{
wChunks[nIndex] = dwSome32BitNumber & 0x0000000f;
dwSome32BitNumber >>= 8;
}
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
Hi David, Can you explain this to me a little more. I think I understand , but I am not exactly sure what the dwSome32BitNumber >> 8 (16,32) or dwSome32BitNumber >>= 8 does exactly with respect to the &0x00000000f. thanks again for your help Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
Hi David, Can you explain this to me a little more. I think I understand , but I am not exactly sure what the dwSome32BitNumber >> 8 (16,32) or dwSome32BitNumber >>= 8 does exactly with respect to the &0x00000000f. thanks again for your help Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
>> and << are right and left bit-shift operators, respectively. Read about them here: http://tinyurl.com/o94p
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
>> and << are right and left bit-shift operators, respectively. Read about them here: http://tinyurl.com/o94p
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
I'm sorry, I posted my message incomplete. I updated the previous post. My question basically is if you are you the bitwise & operator with the first 8bits of the number, won't it end up being 0? So i guess my real question is what does the & 0x00000000f do after the number has been shifted 8 (16, 32)? thank you Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
I'm sorry, I posted my message incomplete. I updated the previous post. My question basically is if you are you the bitwise & operator with the first 8bits of the number, won't it end up being 0? So i guess my real question is what does the & 0x00000000f do after the number has been shifted 8 (16, 32)? thank you Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
NewHSKid wrote: ...what does the & 0x00000000f do... Read about the bitwise-AND operator here: http://tinyurl.com/o96t
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
NewHSKid wrote: ...what does the & 0x00000000f do... Read about the bitwise-AND operator here: http://tinyurl.com/o96t
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
Hi David, I know what the bitwise AND does, thats why i don't understand what you are saying. If I AND all 4 of the word chunks with 0x00000000f, then won't they all just evaluate to 0x00000000f because there is no 1's to match up to? Please correct me, I have a feeling I am not thinking about this correctly. Lets say I get the number as 0xA012B435. Using the bitwise AND will just make it 0. No? Thanks for your help. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
-
Hi David, I know what the bitwise AND does, thats why i don't understand what you are saying. If I AND all 4 of the word chunks with 0x00000000f, then won't they all just evaluate to 0x00000000f because there is no 1's to match up to? Please correct me, I have a feeling I am not thinking about this correctly. Lets say I get the number as 0xA012B435. Using the bitwise AND will just make it 0. No? Thanks for your help. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)
AND means that BOTH bits must be 1 in order for the resulting bit to be 1.
10100000000100101011010000110101
AND 1111101
Continuing to bit-shift the value to the right one word at a time means that you can use 0x0000000f exclusively. You could also have had something like:
DWORD dw = 0xA012B435; WORD w1 = dw & 0x0000000f; WORD w2 = (dw & 0x000000f0) >> 8; WORD w3 = (dw & 0x00000f00) >> 16; WORD w4 = (dw & 000000f000) >> 24;
orWORD w4 = dw & 000000f000; w4 >>= 24;
Whether you bit-shift before or after the AND operation is not important, but doing so beforehand does lend itself to more readable code.
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?