# Category Archives: Bit Magic

## Maximize a given unsigned number number by swapping bits at it’s extreme positions.July 18, 2017

Given a number maximize it by swapping bits at it’s extreme positions i.e. at first and last position, second and second last position and so… Read More »

## Find the smallest number with n set and m unset bitsJuly 17, 2017

Given two non-negative numbers n and m. The problem is to find the smallest number having n number of set bits and m number of… Read More »

## Get the position of rightmost unset bitJuly 13, 2017

Given a non-negative number n. Find the position of rightmost unset bit in the binary representation of n, considering the last bit at position 1,… Read More »

## Josephus Problem Using Bit MagicJuly 12, 2017

The Problem This problem is named after Flavius Josephus a Jewish historian who fought against the Romans. According to Josephus him and his group of… Read More »

## Find the largest number with n set and m unset bitsJuly 10, 2017

Given two non-negative numbers n and m. The problem is to find the largest number having n number of set bits and m number of… Read More »

## Compute the parity of a number using XOR and table look-upJuly 9, 2017

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… Read More »

## Check if binary representations of two numbers are anagramJuly 7, 2017

Given two numbers you are required to check whether they are anagrams of each other or not in binary representation. Examples: Input : a =… Read More »

## Unset bits in the given rangeJuly 6, 2017

Given a non-negative number n and two values l and r. The problem is to unset the bits in the range l to r in… Read More »

## Toggle bits in the given rangeJuly 5, 2017

Given a non-negative number n and two values l and r. The problem is to toggle the bits in the range l to r in… Read More »

## Toggle the last m bitsJuly 4, 2017

Given a non-negative number n. The problem is to toggle the last m bits in the binary representation of n. A toggle operation flips a… Read More »

## Efficiently check whether n is a multiple of 4 or notJuly 3, 2017

Given a number n. The problem is to efficiently check whether n is a multiple of 4 or not without using arithmetic operators. Examples: Input… Read More »

## Count pairs with Odd XOR

Given an array of n integers. Find out number of pairs in array whose XOR is odd. Examples: Input : arr[] = { 1, 2,… Read More »

## Set the rightmost unset bitJuly 1, 2017

Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n. If there are no unset… Read More »

## Previous smaller integer having one less number of set bitsJune 30, 2017

Given a positive integer ‘n’ having ‘x’ number of set bits in its binary representation. The problem is to find the previous smaller integer(greatest integer… Read More »

## Convert a binary number to octal

The problem is to convert the given binary number (represented as string) to its equivalent octal number. The input could be very large and may… Read More »