# Category Archives: Bit Magic

## Quotient and remainder dividing by 2^k (a power of 2)

You are given an positive integer n as dividend and another number m (form of 2^k), you have to find quotient and remainder without performing… Read More »

## Print pair with maximum AND value in an array

Given an array of n positive elements, find the maximum AND value and the pair of elements generating the maximum AND value from the array.… Read More »

## Smallest perfect power of 2 greater than n (without using arithmetic operators)

Given a non-negative integer n. The problem is to find the smallest perfect power of 2 which is greater than n without using the arithmetic… Read More »

## Count set bits using Python List comprehension

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

## Reverse bits using lookup table in O(1) time

Given an unsigned integer, reverse all bits of it and return the number with reversed bits. Examples: Input : n = 1 Output : 2147483648… Read More »

## Variation in Nim Game

Prerequisites: Sprague Gruncy theorem Grundy Numbers Nim is a famous game in which two players take turns removing items from distinct piles. During each turn,… Read More »

## Find largest element from array without using conditional operator

Given an array of n-elements, we have to find the largest element among them without using any conditional operator like greater than or less than.… Read More »

## Unset the last m bits

Given a non-negative number n. The problem is to unset the last m bits in the binary representation of n. Constraint: 1 <= m <=… Read More »

## Check if a number is divisible by 17 using bitwise operators

Given a number n, check if it is divisible by 17 using bitwise operators. Examples: Input : n = 34 Output : 34 is divisible… Read More »

## Bitwise Sieve

Given a number n, print all primes smaller than n. Examples: Input : 30 Output : 2 3 5 7 11 13 17 19 23… Read More »

## Largest number with binary representation is m 1’s and m-1 0’s

Given n, find the greatest number which is strictly not more then n and whose binary representation consists of m consecutive ones, then m-1 consecutive… Read More »

## Maximum AND value of a pair in an array

We are given an array of n positive elements. we need to find the maximum AND value generated by any pair of element from the… Read More »

## Smallest number whose set bits are maximum in a given range

Given a positive integer ‘l‘ and ‘r‘. Find the smallest number ‘n‘ such that l <= n <= r and count of number of set… Read More »

## Check if a number is positive, negative or zero using bit operators

Given a number N, check if it is positive, negative or zero without using conditional statements. Examples: Input : 30 Output : 30 is positive… Read More »

## Minimum flips to make all 1s in left and 0s in right | Set 1 (Using Bitmask)

Given a binary array, we can flip all the 1 are in the left part and all the 0 to the right part.Calculate the minimum… Read More »