Skip to content

Category Archives: Bit Magic

Write a program that unsets the rightmost set bit of an integer. Examples :   Input: 12 (00...01100) Output: 8 (00...01000) Input: 7 (00...00111) Output: 6 (00...00110)… Read More
Given an integer n, find whether it is a power of 4 or not. Example :  Input : 16 Output : 16 is a power… Read More
We need not to do anything if a number is positive. We want to change only negative numbers. Since negative numbers are stored in 2’s… Read More
Compute n modulo d without division(/) and modulo(%) operators, where d is a power of 2 number.  Input: 6 4 Output: 2 Explanation: As 6%4… Read More
On some rare machines where branching is expensive, the below obvious approach to find minimum can be slow as it uses branching. C++ /* The… Read More
Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put… Read More
Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n numbers are occurring twice and the… Read More
Given two numbers A and B. Write a program to count the number of bits needed to be flipped to convert A to B.  Examples: … Read More
  Write an efficient program to count the number of 1s in the binary representation of an integer.Examples :  Input : n = 6Output :… Read More
Given an unsigned integer, reverse all bits of it and return the number with reversed bits. Input : n = 1Output : 2147483648  Explanation :… Read More
  What are these? Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of… Read More
Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in… Read More
Given an array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find… Read More
Write a one-line function to return the position of the first 1 from right to left, in the binary representation of an Integer.  Examples: Input:… Read More
Given a positive integer n, write a function to find if it is a power of 2 or not Examples:  Input : n = 4Output… Read More