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
ES6arrow 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
0areevenwhile integer with last bit as1areoddConverting 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
-
&-ANDOPERATOR : This return1if the corresponding bits ofoperandsare1and return0if 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,EvenNumbers when converted tobitsends with0, JavaScript compares the last bit and discard the remaining. Therefore,0 & 1will return0.
Example 3: The below code checks for odd Number
Explanation
This check if the lastbitis1, then compare1 & 1and returntrueotherwise returnfalse
-
|-OROPERATOR : This return1if any of the corresponding operand'sbitis1and return0is 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.
-
~NOTOperator : This accepts only oneoperand(unary operator).~performsNOToperator on everybit.bitsthat are1become0andbitsthat are0turn 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 bitis0for positive integer and1for negative integer.The remaining
31bitsare used to represent integer.the maximum
32bitsinteger that can be represented can be calculated as2^31 - 2^0 = 2147483647While 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
-
^-XOROPERATOR : Also called (exclusive-Or), it returns0if the operand's bit are the same(0 or 1)and if different return1
Example 6 : The function below performs XOR operation
-
<<-Left shiftOPERATOR : This takes twooperands, thefirstis anintegerto be converted tobitswhile thesecondoperandis the number ofbitsof thefirstoperandto be shifted away from theleftand added to theright. ##### Example 7 : Thefunctionbelow performsLeft shiftoperation
-
>>-Sign-propagating right shiftOPERATOR: This takes twooperands, thefirstis an integer to be converted to bits while thesecondoperandis the number ofbitsof the firstoperandto be shifted away from therightand 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 thatbitsare 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.















Top comments (0)