Bitwise Operation: Left Shift and Right Shift

Left Shift

Logical Left Shift and Arithmetic Left Shift

  • zeros are shifted in to replace the discarded bits
    • inserts value 0 bits into the least significant bit
  • works for both signed as well as unsigned numbers
  • logical and arithmetic left shift have same effect

Left Shift Operator (<<)

new_value = value << count

Same as multiplying value by 2^count (new_value = value * (2^count)) except edge cases (depends on how number is represented in different programming language)

Examples

value = 5      // 00000101
count = 1 

value << count // 00001010 == 10

3              // 00000011
3 << 1         // 00000110 == 3 * 2^1 == 3 *  2 =   6
3 << 2         // 00001100 == 3 * 2^2 == 3 *  4 =  12
3 << 3         // 00011000 == 3 * 2^3 == 3 *  8 =  24
3 << 4         // 00110000 == 3 * 2^4 == 3 * 16 =  48
3 << 5         // 01100000 == 3 * 2^5 == 3 * 32 =  96
3 << 6         // 11000000 == 3 * 2^6 == 3 * 64 = 192

All numbers in JavaScript are 64 bit floating point. However, when bitwise operators are used, JavaScript, internally converts the them into truncated 32 bit signed integers in 2’s complement, then performs the bitwise operation on them before converting the result back to a 64 bit double precision number. 

https://medium.com/@parkerjmed/practical-bit-manipulation-in-javascript-bfd9ef6d6c30
$ node
> 1 << 30
1073741824

> 1073741824 * 2
2147483648

> 1 << 31
-2147483648

Right Shift

Logical Right Shift

  • zeros are shifted in to replace the discarded bits
    • inserts value 0 bits into the most significant bit
  • ideal for unsigned binary numbers

Arithmetic Right Shift

  • the sign bit (bit in most significant bit position) is shifted in on the left, thus preserving the sign of the operand
    • inserts value 0 bits into the least significant bit for positive number
    • inserts value 1 bits into the least significant bit for negative number
  • ideal for signed two’s complement binary numbers

Right Shift Operator (>>)

new_value = value >> count

Same as floor dividing value by 2^count (new_value = value / (2^count)) except edge cases or negative value

Signed or Arithmetic Right Shift Operator (>>)

Examples

3               // 00000011
6 >> 1          // 00000110 >> 1 == 00000011 ==   6 / (2^1) ==   6 / 2  == 3
12 >> 2         // 00001100 >> 2 == 00000011 ==  12 / (2^2) ==  12 / 4  == 3
24 >> 3         // 00011000 >> 3 == 00000011 ==  24 / (2^3) ==  24 / 8  == 3
48 >> 4         // 00110000 >> 4 == 00000011 ==  48 / (2^4) ==  48 / 12 == 3
96 >> 5         // 01100000 >> 5 == 00000011 ==  96 / (2^5) ==  96 / 32 == 3
192 >> 6        // 11000000 >> 6 == 00000011 == 192 / (2^6) == 192 / 64 == 3

3 >> 1          // 00000011 >> 1 == 00000001 == 1
3 >> 2          // 00000011 >> 2 == 00000000 == 0
3 >> 3          // 00000011 >> 3 == 00000000 == 0

// Signed 8-bit number (two's complement)
-1 >> 1         // 11111111 >> 1 == 11111111 == -1
-2 >> 1         // 11111110 >> 1 == 11111111 == -1
-3 >> 1         // 11111101 >> 1 == 11111110 == -2
-4 >> 1         // 11111100 >> 1 == 11111110 == -2
-5 >> 1         // 11111011 >> 1 == 11111101 == -3
-6 >> 1         // 11111010 >> 1 == 11111101 == -3
-7 >> 1         // 11111001 >> 1 == 11111100 == -4
-8 >> 1         // 11111000 >> 1 == 11111100 == -4

Unsigned or Logical Right Shift Operator (>>> for Java, JavaScript)

The programming languages CC++, and Go, however, have only one right shift operator, >>. Most C and C++ implementations, and Go, choose which right shift to perform depending on the type of integer being shifted: signed integers are shifted using the arithmetic shift, and unsigned integers are shifted using the logical shift.

https://en.wikipedia.org/wiki/Logical_shift

Examples

3                // 00000011
6 >>> 1          // 00000110 >>> 1 == 00000011 ==   6 / (2^1) ==   6 / 2  == 3
12 >>> 2         // 00001100 >>> 2 == 00000011 ==  12 / (2^2) ==  12 / 4  == 3
24 >>> 3         // 00011000 >>> 3 == 00000011 ==  24 / (2^3) ==  24 / 8  == 3
48 >>> 4         // 00110000 >>> 4 == 00000011 ==  48 / (2^4) ==  48 / 12 == 3
96 >>> 5         // 01100000 >>> 5 == 00000011 ==  96 / (2^5) ==  96 / 32 == 3
192 >>> 6        // 11000000 >>> 6 == 00000011 == 192 / (2^6) == 192 / 64 == 3

3 >>> 1          // 00000011 >>> 1 == 00000001 == 1
3 >>> 2          // 00000011 >>> 2 == 00000000 == 0
3 >>> 3          // 00000011 >>> 3 == 00000000 == 0

// Signed 32-bit number (two's complement) for JavaScript
-1 >>> 1         // 11111111111111111111111111111111 >>> 1 == 01111111111111111111111111111111 == 2147483647
-2 >>> 1         // 11111111111111111111111111111110 >>> 1 == 01111111111111111111111111111111 == 2147483647
-3 >>> 1         // 11111111111111111111111111111101 >>> 1 == 01111111111111111111111111111110 == 2147483646
-4 >>> 1         // 11111111111111111111111111111100 >>> 1 == 01111111111111111111111111111110 == 2147483646

// Signed 8-bit number (two's complement)
-1 >> 1         // 11111111 >> 1 == 01111111 == 127
-2 >> 1         // 11111110 >> 1 == 01111111 == 127
-3 >> 1         // 11111101 >> 1 == 01111110 == 126
-4 >> 1         // 11111100 >> 1 == 01111110 == 126

Leave a Comment

Your email address will not be published.

4 × three =