So I am trying to follow the tutorials on BitMasks and Bit Manipulation I've seen like this but am not getting expected results.
var a = 0b11110000000000000000000000000000
var b = 0b10010000000000000000000000000000
var c = a & b
console.log(c.toString(2))
// ...
The AND operator will only put a one in every position where all arguments had a one.
expected 0b10010000000000000000000000000000
received -0b01110000000000000000000000000000
Here and other places show how to clear and set a single bit, but I am wondering about a range/span of bits.
- How do I select the set bits in the pattern
0b11110000000000000000000000000000
. So anything that has0b101101010101...
or any other first 6 digits would extract into a number like0b1011010000...
or0b101101
, where either the number was a 6-bit number, or a 32-bit number with all the trailing rhs values set to 0. This way I can do a single check against an integer to check if it has a specific pattern. - How to clear or remove those bits from the integer, so instead of ending up with the 6 lhs bits, I end up with the remaining 26 rhs bits.