# Category Archives: Bit Magic

## Count pairs in an array such that both elements has equal set bits

Given an array arr with unique elements, the task is to count the total number of pairs of elements that have equal set bits count.… Read More »

## Replace every element of the array with BitWise XOR of all other

Given an array of integers. The task is to replace every element by the bitwise xor of all other elements of the array. Examples: Input:… Read More »

## Horizontally Flip a Binary Matrix

Given a binary matrix. The task is to flip the matrix horizontally(find the image of the matrix), then invert it. Note: To flip a matrix… Read More »

## Check if the Xor of the frequency of all digits of a number N is zero or not

Given a number N, the task is to check if xor value of the frequency of the digits is zero or not. Examples: Input: N… Read More »

## Find consecutive 1s of length >= n in binary representation of a number

Given two integers x and n, the task is to search for the first consecutive stream of 1s (in the x’s 32-bit binary representation) which… Read More »

## Replace every array element by Bitwise Xor of previous and next element

Given an array of integers, replace every element with xor of previous and next elements with following exceptions. a) First element is replaced by sum… Read More »

## Find subsequences with maximum Bitwise AND and Bitwise OR

Given an array of n elements. The task is to print the maximum sum by selecting two subsequences of the array (not necessarily different) such… Read More »

## Sorting array elements with set bits equal to K

Given an array of integers and a number . The task is to sort only those elements of the array whose total set bits are… Read More »

## Assign other value to a variable from two possible values

Suppose a variable x can have only two possible values a and b, and you wish to assign to x the value other than its… Read More »

## Number of leading zeros in binary representation of a given number

Given an integer n, output the no. of leading zeros in it’s binary form. A leading zero is any 0 digit that comes before the… Read More »

## Round to next greater multiple of 8

Given an unsigned integer x. Round it up to the next greater multiple of 8 using bitwise operations only. Examples: Input : 35 Output :… Read More »

## Round to next smaller multiple of 8

Given an unsigned integer x. Round it down to the next smaller multiple of 8 using bitwise operations only. Examples: Input : 35 Output :… Read More »

## Minimum numbers needed to express every integer below N as a sum

We have an integer N. We need to express N as a sum of K integers such that by adding some(or all) of these integers… Read More »

## Comparing leading zeros in binary representations of two numbers

Given two Integer numbers x and y. Compare and print which one of them has more leading zeros using Bitwise operation. If both the no.… Read More »

## Check whether bitwise OR of N numbers is Even or Odd

Given an array arr[] containing N numbers. The task is to check whether the bitwise-OR of the given N numbers is even or odd. Examples:… Read More »