## Swap individual bits at given position in an integer

Given an integer, swap consecutive b bits starting from given positions in binary representation of the integer. The bits to be swapped should not overlap with each other.

Coding made easy

Given an integer, swap consecutive b bits starting from given positions in binary representation of the integer. The bits to be swapped should not overlap with each other.

Given a number n, find previous power of 2. If n itself is a power of 2, return n.

Given a number n, find next highest power of 2. If n itself is a power of 2, return n.

Given two 16-bit positive values stored in 32-bit integer variables, find the product using the 8-bit multiply operator that that takes two 8-bit numbers and returns a 16-bit value.

Given two integers, find their minimum and maximum without using branching.

Given an integer, count set bits in it.

Given an integer, calculate its Parity. The parity refers to the number of 1’s in a given binary number. Odd parity (encoded as 1) means there are an odd number of 1’s and even parity (encoded as 0) means that there are an even number of 1’s. Parity bits are often used as a …

Given an integer, count its set bits.

In this post, we will see how to conditionally negate a value without branching. The expression ((flag^(flag-1))*n) negate n when the flag is false and the expression ((n^-flag)+flag) negate n when the flag is true. How does it work? 1. For (flag ^ (flag – 1)) * n if flag = 0, …

Given a number, check if adjacent bits are set in binary representation of it. Naive Solution would be to consider every bit present in the number one by one and compare it with its previous bit. If the current bit is same as previous bit, then we have found a pair whose adjacent bits …

In this post, we will discuss few unrelated problems that can be solved using bit manipulation hacks. 1. Find number of bits needed to be flipped to convert given integer to another The idea is to take XOR of given two integers. After calculating the XOR, the problem will reduces to counting set bits …

Write a program to find XOR of two numbers without using XOR operator. Naive Solution would be to consider every bit present in both numbers one by one (either from left or right) and compare them. If the current bit is same in both numbers (i.e. both are 0 or both are 1), we …