Enhancing Skills

Bitwise Operations in JavaScript

Bitwise Operations

Bitwise operations are used to perform operations on binary representations of numbers. They are essential in game programming for tasks like masking bits, optimizing performance, and manipulating data efficiently.

Command Description

  1. AND (&): Compares each bit of two numbers; returns a new number where each bit is set to 1 only if both corresponding bits are 1.
  2. OR (|): Compares each bit of two numbers; returns a new number where each bit is set to 1 if at least one of the corresponding bits is 1.
  3. XOR (^): Compares each bit of two numbers; returns a new number where each bit is set to 1 if the corresponding bits are different.
  4. NOT (~): Inverts the bits of a number; all 0s become 1s and all 1s become 0s.
  5. Shift Left (<<): Shifts the bits of a number to the left by a specified number of positions, filling with zeros on the right.
  6. Shift Right (>>): Shifts the bits of a number to the right by a specified number of positions, maintaining the sign bit (for negative numbers).
  7. Unsigned Shift Right (>>>): Shifts the bits to the right without maintaining the sign bit; fills with zeros on the left.

Sample Code

// Bitwise AND
let a = 5;  // Binary: 0101
let b = 3;  // Binary: 0011
let andResult = a & b; // Result: 1 (Binary: 0001)
console.log(`Bitwise AND of ${a} and ${b} is ${andResult}`);

// Bitwise OR
let orResult = a | b; // Result: 7 (Binary: 0111)
console.log(`Bitwise OR of ${a} and ${b} is ${orResult}`);

// Bitwise XOR
let xorResult = a ^ b; // Result: 6 (Binary: 0110)
console.log(`Bitwise XOR of ${a} and ${b} is ${xorResult}`);

// Bitwise NOT
let notResult = ~a; // Result: -6 (Binary: ...11111010)
console.log(`Bitwise NOT of ${a} is ${notResult}`);

// Shift Left
let leftShiftResult = a << 1; // Result: 10 (Binary: 1010)
console.log(`Left shift ${a} by 1 is ${leftShiftResult}`);

// Shift Right
let rightShiftResult = a >> 1; // Result: 2 (Binary: 0010)
console.log(`Right shift ${a} by 1 is ${rightShiftResult}`);

// Unsigned Shift Right
let unsignedRightShiftResult = -8 >>> 1; // Result: 2147483644 (Binary: 11111111111111111111111111111000)
console.log(`Unsigned right shift -8 by 1 is ${unsignedRightShiftResult}`);

Output

Bitwise AND of 5 and 3 is 1
Bitwise OR of 5 and 3 is 7
Bitwise XOR of 5 and 3 is 6
Bitwise NOT of 5 is -6
Left shift 5 by 1 is 10
Right shift 5 by 1 is 2
Unsigned right shift -8 by 1 is 2147483644

Use Case

  • Game Development: Bitwise operations are often used for optimizing performance, especially in game engines where speed is critical. For example, they can be employed for masking bits in flags, such as enabling or disabling specific features (e.g., player abilities). Additionally, bit shifting can be used for efficient multiplication or division by powers of two. By manipulating individual bits, developers can achieve more efficient memory usage and faster calculations, which are crucial in performance-sensitive applications like games.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.