Console Learner — An interactive JavaScript course.


2.8 Bitwise Operations

>>>
(0x01 | 0x03).toString(2)
"11"
The | (OR), & (AND), ^ (XOR) and ~ (NOT) bitwise operators uses a 32 bit integer representation of a number. Using toString(2) allows printing the result in binary.
>>>
(0x01 & 0x03).toString(2)
"1"
>>>
(0x01 ^ 0x03).toString(2)
"10"
>>>
(~0xECA86420).toString(16)
"13579bdf"
When using binary negation in particular, care must be taken to avoid conversions to and from the normal 64 bit floating-point representation of numbers, since this will often result in a negative number instead of the expected bit sequence.
>>>
(0xFFFF >> 4).toString(16)
"fff"
The << (left shift), >> (signed right shift) and >>> (unsigned right shift) operators allow moving the bits a number of steps in either direction.
>>>
var a = 1; a <<= 2; print(a);

  
The bitwise operators are also available as assignment operators: |=, &=, ^=, <<=, >>= and >>>=.
>>>

The console allows you to interact with the course material and examples. Use the following keys:

A special logging function is also available:


Need more magic? Check out the other developer tools on this site.