Reversing the bits of an integer

Reversing the bits of an integer means to flip the order of the bits in its binary representation. For example, if you have a 32-bit integer, reversing the bits would mean the bit at position 0 (rightmost bit) moves to position 31 (leftmost bit), the bit at position 1 moves to position 30, and so … Read more

Detecting Power of Two

Detecting whether a given number is a power of two using bit manipulation is an efficient technique that leverages the properties of binary representation. A number is a power of two if it has exactly one bit set in its binary representation. For example, 2 (binary 10), 4 (binary 100), and 8 (binary 1000) are … Read more

Counting bits or Hamming Weight

Counting bits, also known as the “Hamming Weight” or the “population count”, refers to counting the number of 1 bits in the binary representation of a number. This is a common problem in computer science, often used in various applications such as cryptography, algorithms, and network address calculation. In Kotlin, you can solve the counting … Read more

Swap Two variable using Bit Manipulation

Swapping two numbers using bit manipulation involves a technique that makes use of XOR (^) operator. The XOR operator has a unique property where if you XOR any number with itself, the result is 0, and if you XOR any number with 0, the result is the number itself. This property allows swapping without a … Read more