I've got an object of values to build a bit mask from (pardon the CoffeeScript).
flags:
segment: 1 # 00000001
tatum: 2 # 00000010
beat: 4 # 00000100
bar: 8 # 00001000
section: 16 # 00010000
And now I construct a byte in the following manner, using bitwise OR to set specific bits.
byte = 0
byte = byte | flags.segment if data.segment
byte = byte | flags.tatum if data.tatum
byte = byte | flags.beat if data.beat
byte = byte | flags.bar if data.bar
byte = byte | flags.section if data.section
@sendByte(byte)
But the question is, why does this work?
I know that JavaScript stores numbers as double precision floats. Therefore, the binary representation of those number is not like a simple binary integer, right? So, does the bitwise OR |
operator implicitly convert the operands somehow?
For instance, the double 4
, in binary, is:
01000000 00010000 00000000 00000000 00000000 00000000 00000000 00000000
Not this value stated in my comment above:
00000100
And yet the bitwise operators work as if it was 00000010
.
I do know JavaScript sure isn't afraid of implicit conversions. It just seems strange that I'm combining the bits here but those bits are entirely different than the way the values are actually stored in memory. I'm clearly missing something.