Introduction to JavaScript Bitwise Operators
ISIAKA ABDULAHI AKINKUNMI
Posted on August 15, 2021
Introduction
Recently on twitter, a tweep asked how to determine if a number is even or odd. I hurriedly answered using modulus. Yeah, my answer was right, but any other approach? I was hinted about bitwise operator. JavaScript operates using different type of operator which includes
- Assignment operators
- Arithmetic operators
- Ternary operators
- Logical operators
- Comparison operators
- Typeof operator
- Bitwise operators
We commonly use most of these operators in our routine but sparingly use bitwise operator especially if you just getting started with JavaScript.
This article will elucidate on different bitwise operators, how we can use it and
What's bitwise?
Bitwise is a level of operations that involves working with individual bits, which are the smallest units of data in a computer. a bit can be 0
or 1
. All bitwise operations are performed with 32bits
binary numbers and later converted to 64bits(javaScript works with 64bits
).
In this article, we will make use of
ES6
arrow function syntax and conversion to decimal(base 10) will follow the example below:
Conversion of integers to bit will be done using the binary operation as illustrated below, Writing of the bits begins from the lasts bit to the first as pointed by the arrow:
From the above snippets, we can conclude that numbers that has the last bit as
0
areeven
while integer with last bit as1
areodd
Converting 12 to bit , we have
000000000000000000000000000001100
But for simplicity sake, we remove the preceding 0s
and our bits are
1100
Types of Bitwise Operators
&
— AND|
— OR~
— NOT^
— XOR<<
— Left Shift>>
— Sign-Propagating Right Shift>>>
— Zero-Fill Right Shift
Lets get started
-
&
-AND
OPERATOR : This return1
if the corresponding bits ofoperands
are1
and return0
if they differs.
Below is a table for comparison
Example 1: The example below is a function
that computes the AND
operator.
Common interview question is to determine if a number is even or odd, JavaScript Bitwise AND can be used.
Example 2 : Determine if a number is even or odd
Explanation
As I said earlier,Even
Numbers when converted tobits
ends with0
, JavaScript compares the last bit and discard the remaining. Therefore,0 & 1
will return0
.
Example 3: The below code checks for odd
Number
Explanation
This check if the lastbit
is1
, then compare1 & 1
and returntrue
otherwise returnfalse
-
|
-OR
OPERATOR : This return1
if any of the corresponding operand'sbit
is1
and return0
is the operand's bits are0
.
Below is a table illustrating different bits combination
Example 4 : The below code takes two operands
as arguments and perform the OR
bitwise operation.
-
~
NOT
Operator : This accepts only oneoperand
(unary operator).~
performsNOT
operator on everybit
.bits
that are1
become0
andbits
that are0
turn to1
, forming the ones' complement of the given binary value.
Example 5 : The function
below performs NOT
operation
Explanation
As we said earlier, ~ operator turns the bit from zero to one and vice-versa.
~ 8
becomes 1111111111111111111111111111011
(-9 in decimal)
Few things to note:
The first bit by the left is called the
sign bit
. Thesign bit
is0
for positive integer and1
for negative integer.The remaining
31bits
are used to represent integer.the maximum
32bits
integer that can be represented can be calculated as2^31 - 2^0 = 2147483647
While the minimum is
-(2^31) = - 2147483648
Bitwise operators convert their operands to 32-bit signed integers in two’s complement format. when the NOT operator is used on an integer, the resulting value is the two’s complement of the integer as shown below
-
^
-XOR
OPERATOR : Also called (exclusive-Or), it returns0
if the operand's bit are the same(0 or 1)
and if different return1
Example 6 : The function
below performs XOR
operation
-
<<
-Left shift
OPERATOR : This takes twooperands
, thefirst
is aninteger
to be converted tobits
while thesecond
operand
is the number ofbits
of thefirst
operand
to be shifted away from theleft
and added to theright
. ##### Example 7 : Thefunction
below performsLeft shift
operation
-
>>
-Sign-propagating right shift
OPERATOR: This takes twooperands
, thefirst
is an integer to be converted to bits while thesecond
operand
is the number ofbits
of the firstoperand
to be shifted away from theright
and also discarded from theleft
.
Example 8 : The function
below performs Sign-propagating right shift
operation.
-
>>>
-Zero-fill right shift
: This behaves like the sign-propagating right shift (>>) operator. The difference is thatbits
are shifted in from the left.
Example 9 : The function
below performs Zero-fill right shift
operation.
Thanks for Reading!!!!
If you enjoy this and wish to get Notified when I published new articles, click here to subscribe.
Posted on August 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.