0

Bytes: 240 255 255 9 0 224 9 0

f0 ff ff 09 00 E0 09 00

Little endian unsigned int 64 translation:

00 09 E0 00 09 ff ff f0

int value1 = 0-19 bits  
int value2 = 20-39 bits 
int value3 = 40-59 bits
int value4 = 60-62 bits 
bool value5 = 63 bit

value1 = (uint)(byteArray[0] | byteArray[1] << 8 | (byteArray[2] << 16)) & 0x14;

Am I doing this correctly? I keep getting value of 0 but should be 158.

5
  • 1
    What is the intent of the & 0x14? That might be your problem. Commented Jul 22, 2013 at 17:10
  • 1
    Also: why are you trying to store 64 bits in 5 ints? Why not byte[] or long/ulong? Commented Jul 22, 2013 at 17:12
  • Are you trying to store RGBA - value with 20bits per channel (20-20-20-4) or something similar? 0x14 should be about 5 "F" - 0xFFFFF... or your braces are wrong as The Moof suggested. Commented Jul 22, 2013 at 17:24
  • I'm parsing a file which has byte array of size 8. I have to parse 5 variables from the 8 bytes. Commented Jul 22, 2013 at 17:25
  • ((byteArray[0] | byteArray[1] << 8 | (byteArray[2] << 16))) & 0xfffff this still gives me wrong value. Commented Jul 22, 2013 at 17:26

1 Answer 1

1

The last operation in your calculation is & 0x14. This will do a bitwise and against the binary value of 0001 0100. You're looking for the first 20 bits, so your mask should be 0xfffff.

Sign up to request clarification or add additional context in comments.

5 Comments

yeah I was trying to mask off the 20 bits because it was currently 24 bits long. So i assume 0x14 being 20 in decimal, it would mask it. (uint)(byteArray[0] | byteArray[1] << 8 | (byteArray[2] << 16)) & 0xfffff; still gives me 2304.
Based on your rules, the first number would be 0x00900, which is 2304.
Keep in mind that bit values are read right-to-left. Based on your expected answer, it leads me to believe that the rules are incorrect. They should be Val1 = bits 44-63, Val2 = bits 24-43, etc. Using these rules with the Little Endian bytes, you will get the expected values.
The specification sheet for this particular file(which I can't specify what it is), says that's how the bits are stored and what order.
The bit order isn't the problem. As I said, numbering your bits backwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.