One Pager Cheat Sheet
- We usually represent numbers in decimal notation (Base 10), while computers prefer binary numbers (Base 2) represented by a sequence of ones and zeros, where each bit represents a power of decimal 2.
- Because of its convenience for logic circuits and its use for bitwise operations in programming, binary is the basis for all modern computer hardware and software.
- Javascript supports 7 bitwise operators; the four bitwise logical operators,
&
,|
,^
, and~
, and the three bitwise shift operators,<<
,>>
, and>>>
, which allow binary values to be manipulated and converted to decimal numbers. - Bitwise operators
&
,|
,^
, and~
manipulate the bits of their operands and the resulting bits are converted back to a decimal form. - Bitwise shift operators in JavaScript
shift
bits left or right by a predetermined number of bit positions, and the choice of operator determines whether the leftmost bit defines the sign of the number or it is filled with zeros. - Despite some common use cases in
image editing
,motion graphics
,data compression and encryption
,device drivers
, andembedded programming
, direct bit manipulation is not a common industry practice, and should only be used if it brings added value. - The left shift operator (
<<
)shifts
the binary sequence of a number to the left, potentially changing its sign. - Answer this interview question with efficiency by using Bitwise Operators in the
XOR swap algorithm
. - You can use Bitwise AND to quickly check if an integer
x
is even or odd without using division:x & 1
will return1
if the integer is odd, and0
if it's even. - Shifting
n
digits to the left is equivalent to multiplying by2
, which is achieved by binary multiplication ofx
multiplied by2
to the powern
. - Check if a positive integer is a power of 2 without branching by subtracting 1 from it and performing a Bitwise AND operation.
- The
XOR
bitwise operatorcompares
each bit of two integers, returning1
if they are different and0
if they are the same, allowing for the detection of opposite signs and beingsymmetric
regardless of the order of the two numbers being compared. - Learn more about
bitwise operators
and their usage in real-world scenarios by exploring these resources.