# Category Archives: Bit Magic

## Count set bits in an integer using Lookup Table

Write an efficient program to count number of 1s in binary representation of an integer. Examples Input : n = 6 Output : 2 Binary… Read More »

## Set all even bits of a number

Given a number, the task is to set all even bit of a number. Positions of bits are counted from LSB (least significant bit) to… Read More »

## Sum of XOR of all pairs in an array

Given an array of n integers, find the sum of xor of all pairs of numbers in the array. Examples : Input : arr[] =… Read More »

## Number of Reflexive Relations on a Set

Given a number n, find out number of Reflexive Relation on a set of first n natural numbers {1, 2, ..n}. Examples : Input :… Read More »

## Set all odd bits of a number

Given a number, the task is to set all odd bits of a number. Positions of bits are counted from LSB (least significant bit) to… Read More »

## Alternate bits of two numbers to create a new number

Given two numbers, the task is to use alternative bits within two numbers to create result. We take first bits of second number, then second… Read More »

## Decimal representation of given binary string is divisible by 20 or not

The problem is to check whether the decimal representation of the given binary number is divisible by 20 or not. Take care, the number could… Read More »

## For every set bit of a number toggle bits of other

Given two integer numbers, whenever the bits of the first number is set i.e. 1, toggle the bits of the second number leaving the rest… Read More »

## Print numbers having first and last bits as the only set bits

Given a positive integer n. The problem is to print numbers in the range 1 to n having first and last bits as the only… Read More »

## Toggle bits of a number except first and last bits

Given a number, the task is to toggle bits of the number except the first and the last bit. Examples: Input : 10 Output :… Read More »

## Check if given four integers (or sides) make rectangle

Given four positive integers, determine if there’s a rectangle such that the lengths of its sides are a, b, c and d (in any order).… Read More »

## Find number of pairs in an array such that their XOR is 0

Given an array of size N. Find the number of pairs (i, j) such that XOR = 0, and 1 <= i < j <=… Read More »

## Toggle all even bits of a number

Given a number, the task is to Toggle all even bit of a number Examples: Input : 10 Output : 0 binary representation 1 0… Read More »

## Toggle first and last bits of a number

Given a number n, the task is to toggle only first and last bits of a number Examples : Input : 10 Output : 3… Read More »

## Check if actual binary representation of a number is palindrome

Given a non-negative integer n. The problem is to check if binary representation of n is palindrome or not. Note that the actual binary representation… Read More »