Re: Bitwise operator not working as expected.
#####
Reply #6 –

This isn't quite what you are asking for, but using Hexadecimal literals is how it's often done (even in low level languages, like C++ and C).

Hexadecimal is pretty handy for representing binary. It has the advantage over parseInt() that it can be written as literals (just the number, no function calls), and the disadvantage that it is not, in fact, binary.

To write hex, you put a leading '0x' in front. Like this:

var num = 0xFF;

//same as 1111 1111 binary, and 255 in decimal.

//test the third bit

var mask = 0x04;

//same as 0000 0100 binary, note the third bit is a 1 and all others are 0's.

if(mask&num){

//do things!

}

The calculators on most OS's (and DE's in teh Linuxes) can convert binary, decimal, and hexadecimal for you. The advantage of Hex over decimal for representing a binary number is that each digit in hex is four digits in binary, so it lines up well (very much unlike decimal, sort of unlike octal). Plus, you only have to memorize 16 digits of hex to know the representation in binary without any help from calculators or converters.