Hacks on Bitwise Programming

Facts and Hacks in JavaScript

Trung Anh Dang
Mar 25 · 5 min read
Photo by Sergey Zolkin on Unsplash

In programming, we can work with bits effectively. First, let us see the definitions of each bit operation and then move onto different techniques for solving the problems. Basically, there are six operators that every language support for bit manipulation: & (AND), | (OR),^ (XOR), << (left-shift), >> (right-shift), and ~ (complement).

Facts on Bitwise Programming

Bitwise AND

// a = 10 (in binary)
var a = 2
// b = 11 (in binary)
var b = 3
// a & b = 10 & 11 = 10 (in binary) = 2 (in decimal)
a & b
<< 2

Bitwise OR

// a | b = 10 | 11 = 11 (in binary) = 3(in decimal)
a | b
<< 3

Bitwise XOR

// a ^ b = 10 ^ 11 = 01 (in binay) = 1 (in decimal)
a ^ b
<< 1

Bitwise Left Shift

// c = 0101 (in binary)
var c = 5
// bitwise left shift: c << 1 = 1010 (in binary) = 10 (in decimal)
c << 1
<< 1

Bitwise Right Shift

// c >> 1 = 010 (in binary) = 2 (in decimal)
c >> 1
<< 2

Bitwise Complement

// ~c -> 1010 (in binary) -> 10 (in decimal), 2’s compliment of 10 will be -6.
~c
<< -6

Hacks on Bitwise Programming

Checking Whether Number is Power of 2 or Not

// n = 10000
var n = 16 // 2⁴
// n — 1 = 15 (in decimal) = 01111
// n & (n — 1) = 10000 & 01111 = 0
n & (n — 1)
<< 0

Multiplying Number by Power of 2

var n = 16// 16*(2^1) = 32
n << 1
<< 32

Dividing Number by Power of 2

var n = 16// 16/(2²) = 4
n >> 2
<< 4

Performing Average without Division

We can use mid = (low + high) >> 1 .

Finding Modulo of a Given Number

var n = 20// 20%8 = 4
n & 0x7
<< 4

Similarly, we can find modulo value of any number.

Checking Whether K-th Bit is Set or Not

// n = 01001011 (in binary)
var n = 75
var k = 4
// 1 << k — 1 = 00001000 (in binary)
// n & (1 << k — 1) = 01001011 & 00001000 = 00001000 = 8 (in decimal)
n & (1 << k — 1)
<< 8

Setting K-th Bit

// n = 01001011 (in binary)
var n = 75
var k = 3
// 1 << k — 1 = 00000100 (in binary)
// n | (1 << k — 1) = 01001011 | 00000100 = 01001111 (in binary) = 79 (in decimal)
n | (1 << k — 1)
<< 79

Clearing K-th Bit

// n = 01001011 (in binary)
var n = 75
var k = 3
// 1 << k — 1 = 00000100 (in binary)
// n & (1 << k — 1) = 01001011 & 00000100 = 0 (in binary) = 0 (in decimal)
n & (1 << k — 1)
<< 0

Toggling k-th bit of a number

// n = 01001011 (in binary) -> toggle(n, k) = 01001111
var n = 75
var k = 3
// 1 << k — 1 = 00000100 (in binary)
// n ^ (1 << k — 1) = 01001011 ^ 00000100 = 01001111 (in binary) = 79 (in decimal)
n ^ (1 << k — 1)
<< 79

Toggling Rightmost One Bit

// n = 01001011 (in binary)
var n = 75
// n — 1 = 74 = 01001010
// n & n — 1 = 01001011 & 01001010 = 01001010 = 74
n & n — 1
<< 74

Thanks for reading 😘, goodbye 👋, and don’t forget to 👏 up to 50 times and follow!

JavaScript in Plain English

Learn the web's most important programming language.

Trung Anh Dang

Written by

I write about things that I like and things that I don’t, mainly in the business, art and tech sphere

JavaScript in Plain English

Learn the web's most important programming language.

More From Medium

More from JavaScript in Plain English

More from JavaScript in Plain English

More from JavaScript in Plain English

32 funny Code Comments that people actually wrote

10.3K

More from JavaScript in Plain English

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade