Languages
[Edit]
EN

JavaScript - bitwise OR operator

8 points
Created by:
Imaan-Morin
1009

The main goal of this articleĀ is to show how bitwise OR operationĀ (| operator) works on the JavaScript example.

The main concept of theĀ bitwise ORĀ operationĀ is to make OR operations on each correspondingĀ bits.

Quick solution:

// ONLINE-RUNNER:browser;

const a = 3;  // 0011
const b = 5;  // 0101

const result = a | b;  // <----- bitwise OR operation example

console.log(result);                               // 7    (in dec / base 10)
console.log(result.toString(2).padStart(4, '0'));  // 0111 (in bin / base 2 )

Ā 

The truth table for the OR operation is:Ā 

aba OR b (in JavaScript: a | b)JS Example
0000 | 0 === 0
0110 | 1 === 1
1011 | 0 === 1
1111 | 1 === 1

So, the above table on 1 bit numbers can be presented in JavaScript as:

// ONLINE-RUNNER:browser;

console.log(0 | 0);  // 0
console.log(0 | 1);  // 1
console.log(1 | 0);  // 1
console.log(1 | 1);  // 1

Ā 

More complex example

// ONLINE-RUNNER:browser;

const a = 3;  // 00000011
const b = 5;  // 00000101

const result = a | b;  // <----- bitwise OR operation example

                                  // (in dec / base 10)
console.log(a);                   // 3   
console.log(b);                   // 5
console.log(result);              // 7

                                  // (in bin / base 2 )
console.log(getBits(a, 8));       // 00000011
console.log(getBits(b, 8));       // 00000101
console.log(getBits(result, 8));  // 00000111


// Helpers:

function getBits(number, length) {
	let text = '';
  	for (let i = length - 1; i > -1; --i) {
    	text += 0x01 & (number >> i);
    }
  	return text;
}

Ā 

Bitwise ORĀ operation limitations

Hint: in JavaScript, bitwise OR works on 32-bit integers. If number is bigger, the most significant bits are dicarded to get 32-bit integer number - it means only bits indexed from 0 to 31 will be used.

So, the aboveĀ table can be also written as:

    3 (base 10) = 00000000000000000000000000000011 (base 2)
    5 (base 10) = 00000000000000000000000000000101 (base 2)
                  --------------------------------
3 | 5 (base 10) = 00000000000000000000000000000001 (base 2) = 7 (base 10)

Ā 

Features

1. bitwise ORĀ operation is fast - easy to do by any CPU.

See also

  1. JavaScript - bitwise operators

  2. JavaScript - bitwise NOT example

  3. JavaScript - bitwise AND example

  4. JavaScript - bitwise XOR example

  5. JavaScript - get supported max integer value

References

  1. Logical disjunction (OR) - Wikipedia
  2. Bitwise operation - Wikipedia

Alternative titles

  1. JavaScript - bitwise OR (|) operator example
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join