# Category Archives: Bit Magic

## Count number of bits to be flipped to convert A to BSeptember 5, 2009

Suggested by Dheeraj Question: You are given two numbers A and B. Write a program to count number of bits needed to be flipped to convert A to B.

## Count set bits in an integerAugust 19, 2009

Write an efficient program to count number of 1s in binary representation of an integer. Examples Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits 1.… Read More »

## Write an Efficient C Program to Reverse Bits of a NumberAugust 2, 2009

Given an unsigned integer, reverse all bits of it and return the number with reversed bits. Input : n = 1 Output : 2147483648 On a machine with size of unsigned bit as 32. Reverse of 0….001 is 100….0. Input : n = 2147483648 Output : 1 Method1 – Simple Loop through all the bits… Read More »

## Little and Big Endian MysteryJuly 16, 2009

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 binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first.

## Check for Integer OverflowJune 29, 2009

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 “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.

## Find the Number Occurring Odd Number of TimesJune 22, 2009

Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.

## Position of rightmost set bitJune 19, 2009

Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer. I/P 18, Binary Representation 010010 O/P 2 I/P 19, Binary Representation 010011 O/P 1

## Write one line C function to find whether a no is power of twoMay 30, 2009

Given a positive integer, write a function to find if it is a power of two or not. Examples: Input : n = 4 Output : Yes 22 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 25 = 32 We strongly recommend that you click… Read More »

## Efficient way to multiply with 7

We can multiply a number by 7 using bitwise operator. First left shift the number by 3 bits (you will get 8n) then subtract the original numberfrom the shifted number and return the difference (8n – n).

## Write a C program to find the parity of an unsigned integer

Parity: Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits. Main idea of the below solution is – Loop while n is not 0 and… Read More »