JavaScript Canvas Bit Manipulation |
Written by Ian Elliot | |||||||||||
Monday, 09 August 2021 | |||||||||||
Page 1 of 2 If you are going to work with bitmaps at the most basic level you have to know how to test and modify bit patterns. The reason is that each pixel is stored using a set of bits to record each of the color channels.. Now available as a paperback or ebook from Amazon.JavaScript Bitmap Graphics
|
AND |
& |
OR |
| |
XOR (exclusive or) |
^ |
Not |
~ |
As you would expect, the NOT operator has the highest priority.
Notice that there are also corresponding logical operators &&, || and ! that only work with Boolean values and not bit patterns.
If you are more familiar with other languages you might well confuse ^ with raise to a power.
The bitwise operators work with numeric data which is converted from floating point to a 32-bit integer, operated on and then converted back to floating point. As a floating double can store a 32-bit integer without loss of precision everything works transparently as long as you stay within the 32-bit limit.
For example:
var a = 0xF0; var b = 0xFF; var c = ~a & b; alert(c);
This first works out the bitwise NOT of a, i.e. 0F. This is then bitwise ANDed with b, i.e. 0F & FF, which is F. You should see the result 15 in decimal displayed. Bit manipulation is usually easiest to try out using hexadecimal notation with the results returned in decimal, 0xF is 15, or in binary, 0xF is 1111. There is no way of entering binary directly in JavaScript but you can use hex and octal and there is the paresInt function which will convert from a range of bases.
So what happens if you go over the 32-bit limit?
In fact, strange things start to happen before you reach the 32-bit limit because the 32-bit value is signed and this means that if the highest bit is 1 the returned value is negative.
For example:
var a = 0xFFFFFFF; var b = 0xFFFFFFF; var c = a & b; alert(c.toString(16));
displays 0xFFFFFFF, which is what you would expect. Notice that toString(16) converts the floating point number to a hex string.
Now try adding one more F to both values i.e. a full 32-bit value all set to 1. The result displays as -1 which might not be what you expect but it is perfectly correct.
The result of ANDing two full 32-bit values both set to 1 is a 32-bit value with all bits set to one but as the 32-bit value is interpreted as a signed value when converted to floating point this displays as -1 using two’s complement.
You don't have to worry about this too much as the bit pattern corresponding to -1 is 32 bits all set to 1, so everything should carry on working if you use the value for further bit manipulation. That is, although it looks wrong it still works as far as bit manipulation is concerned.
Now consider what happens if you add one more F to the values. In this case the values are 36 bits, all set to 1 and the result should also be 36 bits, all set to 1. But no, both the a and b values are converted to 32-bit values, the two values are ANDed together and give a 32-bit result, i.e. -1 as before. You can’t do bit manipulation with more than 32 bits.