Broad Network


ECMAScript Bitwise Operators

ECMAScript Bitwise Operators

Writing ECMAScript Module

Foreword: In this tutorial I talk about the bitwise NOT operator, bitwise AND operator, bitwise OR operator and bitwise Exclusixe OR operator; I also talk about bitwise shift operators.

By: Chrysanthus Date Published: 24 Jul 2016

Introduction

In this tutorial I talk about the bitwise NOT operator, bitwise AND operator, bitwise OR operator and bitwise Exclusixe OR operator; I also talk about bitwise shift operators. Bitwise operators operate on bits. However, you have to represent the bits as a decimal number (base 10) before the bitwise operators can operate on. The return value is a decimal number, which you have to convert to bits. In this series we consider only unsigned numbers. You need to have Node.js installed in order to test the code samples.

Bitwise Logical Operators

Bitwise NOT Operator
The bitwise NOT operator is ~. The bitwise NOT of the byte,

    01001101

is

    10110010

Remember, 1 means true and 0 means false. The binary number 01001101 is the decimal 77. The binary number 10110010 is the decimal 178. So, if we bitwise NOT 77, we should have 178. The following code does this with the computer:

    opernd = 0b01001101;

    answer = ~opernd;

    console.log(answer);

0b means the text on the right is binary (base 2) and implies conversion to base 10. I tried the code and I had –78, which is the byte signed number of 178. The operation is correct.

Bitwise AND
The bitwise AND operator is &. The following is a manual bitwise AND operation:

00110110
01010100
--------
00010100
========

The following code does this with the computer:

    operandL = 0b00110110;

    operandR = 0b01010100;

    answer = operandL & operandR;

    console.log(answer);

I tried this in my computer and I had 20 in base 10. Decimal 20 is 00010100 in base 2. So the operation is correct.

Bitwise OR
The bitwise OR operator is |. The following is a manual bitwise OR operation:

10111010
00101001
--------
10111011
========

The following code does this with the computer:

    operandL = 0b10111010;

    operandR = 0b00101001;

    answer = operandL | operandR;

    console.log(answer);

I tried this in my computer and I had decimal 187. Decimal 187 is binary 10111011.

Bitwise XOR
Bitwise exclusive OR is OR where true OR true is false (the other OR actions remaining the same). The bitwise XOR operator is ^. The following is a manual bitwise XOR operation:

10111010
00101001
--------
10010011
========

The following code does this with the computer:

    operandL = 0b10111010;

    operandR = 0b00101001;

    answer = operandL ^ operandR;

    console.log(answer);

I tried this in my computer and I had decimal 147. Decimal 147 is binary 10010011.

Bitwise Shift Operators

Left Shift Operator
The left shift operator is, <<. It shifts all the bits to the left, a number of places, adding corresponding number of zeroes to the right. The syntax is:

    operand << n

where n is the number of places to be shifted. The left shift of,

    11101011

by 2 places is

    1110101100

The following code does the above 2 place shifting, in the computer:

    opernd = 0b11101011;

    answer = opernd << 2;

    console.log(answer);

I tried the code in my computer and I had decimal 940, which is binary 1110101100.

The above number shifted, is a byte (8 bits). There would be times when you will want to discard the extra result bits on the left. To achieve this, shift the byte while it is in an Uint8Array buffer. The following code illustrates this:

    arrBuf = new ArrayBuffer(1);
    t8Arr = new Uint8Array(arrBuf);
    t8Arr[0] = 0b11101011;

    t8Arr[0] = t8Arr[0] << 2;

    console.log(t8Arr[0]);

Note that the result goes back to the same buffer cell. I tried this in my computer and I had decimal 172, which is binary 10101100 with the extra result bits on the left discarded.

Right Shift Operator
The right shift operator is, >>>. It shifts all the bits to the right, a number of places, adding corresponding number of zeroes to the left. The syntax is:

    operand >>> n

where n is the number of places to be shifted. The right shift of,

    11101011

by 2 places is

    00111010

with the extra result bits on the right discarded.

The following code does the above 2 place shifting, in the computer:

    opernd = 0b11101011;

    answer = opernd >>> 2;

    console.log(answer);

I tried the code in my computer and I had decimal 58, which is binary 00111010.

That is it for this tutorial.

Chrys

Related Links

Internet Socket and ECMAScript
Handling Bytes in ECMAScript
ECMAScript Bitwise Operators
ECMAScript Module Essentials
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

Comments

Become the Writer's Follower
Send the Writer a Message