2

I am working on parsing some binary files, I have them opened and in an ArrayBuffer.

In the particular file structure I am reading, there are a number of bits which are boolean and I can check whether they are checked with:

(flag & 1) != 0; // bit 0 
(flag & 2) != 0; // bit 1 
(flag & 4) != 0; // bit 2 

etc.

However, I am having trouble getting the values of the bits followed. They span over multiple bits (for example bits 4-6) and consist of an integer value from 0-7.

How are multiple bits read like that? I understand that this isn't as much of a JavaScript question than that of how bits and bitwise operators work.

0

2 Answers 2

4

Assuming you want 4-6 bits from a byte like this:

76543210
 ^^^

You would construct a bit mask like this:

0x70

which means:

01110000

And then you would & that with the number and shift to right 4 times:

( byte & 0x70 ) >> 4
//Number between 0-7
Sign up to request clarification or add additional context in comments.

9 Comments

In javascript, I would still always recommend shifting with >>> instead of >>. (In C, cast to unsigned before shifting).
@selbie I didn't mention this in my question, but if I've read the byte as Uint, it will be unsigned already?
the & already drops out potential sign bit, which is in position 31
There is no such thing as an "unsigned integer" in javascript. There is a "number" type, and that is it. So Mike, I'm not sure how you are reading a UINT in javascript. @Esailija - where is the behavior of & operator making the value unsigned referenced? Or is it just a side effect of javascript not really having an upper bound on number types?
@selbie, all numbers are converted to signed 32 bit integers (with exception >>>, converts to unsigned 32) in javascript before the bitwise operation. So if it was a signed 8 bit integer -127, it would be 0xFFFFFF81before the & operation, and 0x0000000 after in that case. In any case, the sign bit is not persisted through the AND.
|
2

Assuming the least significant bit is at position "0", and you want the 3-bit integer between bit positions 4-6.

var value = (flag >>> 4) & 0x0007;

In other words, right shift "flag" 4 bits to the right, such that bits 4-6 get shifted into positions 0-2. Then mask off just the last three bits (binary 111 = decimal 7).

1 Comment

I updated my answer below to use the javascript "unsigned shift" operator (>>>) instead of the more usual >> operator. Always a good idea in any language to make sure you are doing bit-shifting on unsigned types, because you might get a side-effect if the value being shifted is negative (1's imported on the left side instead of 0's).

Your Answer

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