Bitwise operators (JavaScript)

Bitwise operators perform bit-by-bit logical operations.

Operator Description
a & b Bitwise AND. Returns 1 where the corresponding bits in a and b are 1. Returns 0 where either bit is 0.
a | b Bitwise OR. Returns 1 where either of the corresponding bits in a and b is 1, or both are 1. Returns 0 where both bits are 0.
a ^ b Bitwise XOR (exclusive OR). Returns 1 where either of the corresponding bits in a and b is 1, and the other is 0. Returns 0 where both bits are 0.
~a Negation. Inverts the bits of a.
a << n Left shift. Shifts the bits in a to the left n positions, discarding bits that fall off on the left, and filling vacated right positions with 0.
a >> n Right shift with sign preserved. Shifts the bits in a to the right n positions, discarding bits that fall off on the right, and filling vacated left positions with 0, except that the leftmost bit in a is preserved.
a >>> n Right shift. Shifts the bits in a to the right n positions, discarding bits that fall off on the right, and filling vacated left positions with 0.

Usage

The bitwise logical operators treat the operands and result as a series of 32 bits (like an integer).

AND, OR, and XOR act upon the corresponding bits in the operands and the result. Bit 0 of the first operand combines with bit 0 of the second operand to produce bit 0 of the result; bit 1 of the first operand combines with bit 1 of the second operand to produce bit 1 of the result; and so on.

The negation operator acts upon the corresponding bits in the operand and the result.

Examples

This example exercises the bitwise operators with the integer values 9, 5, and -9 as operands. Integer 9 has the bit configuration 1001 preceded by 28 zeros. Integer 5 has the bit configuration 0101 preceded by 28 zeros. Integer -9 has the bit configuration 0111 preceded by 28 ones.
function p(stuff) {
	print("<<<" + stuff + ">>>");
}

var a = 9; // 0x00000009 bin 0...1001
var b = 5; // 0x00000005 bin 0...0101

p("a & b = " + (a & b)); // 1 0x00000001 bin 0...0001
p("a | b = " + (a | b)); // 13 0x0000000D bin 0...1101
p("a ^ b = " + (a ^ b)); // 12 0x0000000C bin 0...1100
p("~a = " + (~a)); // -10 0xFFFFFFF6 bin 1...0110
p("a << 2 = " + (a << 2)); // 36 0x00000024 0...100100
p("a >> 2 = " + (a >> 2)); // 2 0x00000002 0...0010
p("a >>> 2 = " + (a >>> 2)); // 2 0x00000002 0...0010
p("a >> 2 = " + (-a >> 2)); // -3 0xFFFFFFFD 1...1101
	// -9 is 1...0111 >> 2 = 1...1101

This example demonstrates a function that takes an integer as a parameter and returns its string equivalent in binary format. It shifts the bits one at a time to the right and compares the rightmost bit to 1 (bit on) to construct the return value.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

function binaryString(n) {
	var nn = n;
	var ss = "";
	for(var i = 0; i < 32; i++) {
		ss = (1 & nn) + ss;
		nn = nn >> 1;
	}
	return ss;
}

p(binaryString(1));
p(binaryString(15));
p(binaryString(128));
p(binaryString(255));
p(binaryString(4096));

This example demonstrates a function that takes an integer as a parameter and returns its string equivalent in hexadecimal. It shifts the bits four at a time to the right and calculates the hexadecimal equivalent of the right four bits to construct the return value.

function p(stuff) {
	print("<<<" + stuff + ">>>");
}

function hexString(n) {
	var nn = n;
	var ss = "";
	for(var i = 0; i < 8; i++) {
		switch(15 & nn) {
			case 15 : ss = "F" + ss; break;
			case 14 : ss = "E" + ss; break;
			case 13 : ss = "D" + ss; break;
			case 12 : ss = "C" + ss; break;
			case 11 : ss = "B" + ss; break;
			case 10 : ss = "A" + ss; break;
			default : ss = (15 & nn) + ss;
		}
		nn = nn >> 4;
	}
	return ss;
}

p(hexString(1));
p(hexString(13));
p(hexString(12));
p(hexString(-10));
p(hexString(36));
p(hexString(2));
p(hexString(2));
p(hexString(-3));
p(hexString(1));
p(hexString(15));
p(hexString(16));
p(hexString(62));
p(hexString(128));
p(hexString(255));
p(hexString(2020));
p(hexString(4095));
p(hexString(4096));