Master Bitwise Operations: Essential Techniques and Real-World Examples
Bitwise operations manipulate binary digits directly, offering fast, low‑level computation for tasks such as arithmetic optimization, parity checks, power‑of‑two detection, swapping values, counting set bits, bitmasking, and hash table enhancements, with clear examples and Go code illustrations throughout the guide.
Bitwise Operations Overview
Bitwise operations manipulate the binary representation of data directly. Because all data is stored as bits, these operations are extremely fast and are useful in performance‑critical code.
Common Bitwise Operators
AND (&)
Returns 1 only when both corresponding bits are 1; otherwise 0.
1010 (10)
& 1100 (12)
--------
1000 (8)1 & 1 = 1
0 & 1 = 0
1 & 0 = 0
0 & 0 = 0
OR (|)
Returns 1 when at least one of the corresponding bits is 1; returns 0 only when both bits are 0.
1010 (10)
| 1100 (12)
--------
1110 (14)XOR (^)
Returns 1 when the two bits differ and 0 when they are the same.
1010 (10)
^ 1100 (12)
--------
0110 (6)NOT (~)
Flips every bit: 0 becomes 1 and 1 becomes 0. Sign‑bit handling depends on the integer representation.
~1010 (10) => 0101 (‑11) # example for two's‑complementLeft Shift (<<)
Shifts all bits left by a specified number of positions, filling the right side with zeros. Effectively multiplies the number by 2ⁿ.
1010 (10) << 2 = 1000 (40)Right Shift (>>)
Shifts all bits right by a specified number of positions. For unsigned numbers it divides by 2ⁿ; for signed numbers the sign bit may be preserved.
1010 (10) >> 2 = 0010 (2)Practical Applications
Fast Arithmetic Optimization
Left shift can replace multiplication by powers of two, and right shift can replace division by powers of two.
// Multiply by 2 using left shift
x := 5
x = x << 1 // equivalent to x = x * 2Parity Checking (Odd/Even)
Using AND with 1 isolates the least‑significant bit, allowing a quick test for odd numbers.
func isOdd(x int) bool {
return x & 1 == 1
}Power‑of‑Two Detection
An integer is a power of two when it has exactly one set bit. The expression x & (x-1) clears the lowest set bit; if the result is zero, the number is a power of two.
func isPowerOfTwo(x int) bool {
return x > 0 && (x & (x - 1)) == 0
}Swapping Two Numbers Without a Temporary Variable
Using XOR, two variables can be exchanged without extra storage.
func swap(x, y int) (int, int) {
x = x ^ y
y = x ^ y
x = x ^ y
return x, y
}Counting Set Bits
Repeatedly applying x & (x-1) clears the lowest set bit, allowing an efficient count of ones.
func countOnes(x int) int {
count := 0
for x > 0 {
count++
x = x & (x - 1) // clear lowest set bit
}
return count
}Bitmasking
Bitmasks use individual bits to represent multiple boolean flags within a single integer.
const (
Flag1 = 1 << iota // 0001
Flag2 // 0010
Flag3 // 0100
Flag4 // 1000
)
flags := Flag1 | Flag3 // set Flag1 and Flag3
if flags & Flag1 != 0 {
fmt.Println("Flag1 is set")
}Hash Table Optimizations
Bitwise tricks can improve hash functions and collision handling, compressing data into smaller indices for faster look‑ups.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
