# Category Archives: Bit Magic

## Check whether all the bits are set in the given rangeSeptember 7, 2017

Given a non-negative number n and two values l and r. The problem is to check whether all the bits are set or not in… Read More »

## 1 to n bit numbers with no consecutive 1s in binary representationSeptember 3, 2017

Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation. Examples:- Input… Read More »

## Find the n-th number whose binary representation is a palindromeAugust 31, 2017

Find the nth number whose binary representation is a palindrome. Do not consider the leading zeros, while considering the binary representation. Consider the 1st number… Read More »

## Shuffle a pack of cards and answer the queryAugust 30, 2017

Given a pack of 2^N cards (0 … 2^N – 1), shuffle it in N steps. At step k (0 < k < N) we… Read More »

## Extract ‘k’ bits from a given position in a number.August 21, 2017

How to extract ‘k’ bits from a given position ‘p’ in a number? Examples: Input : number = 171 k = 5 p = 2… Read More »

## Efficiently find first repeated character in a string without using any additional data structure in one traversal

Implement a space efficient algorithm to check First repeated character in a string without using any additional data structure in one traversal. Use additional data… Read More »

## Change all even bits in a number to 0August 15, 2017

Given a number, change all bits at even positions to 0. Examples: Input : 30 Output : 10 Binary representation of 11110. Bits at Even… Read More »

## Computing INT_MAX and INT_MIN with Bitwise operationsAugust 9, 2017

Prerequisites : INT_MAX and INT_MIN in C/C++ and Applications. Arithmetic shift vs Logical shift Suppose you have a 32-bit system : The INT_MAX would be… Read More »

## Add minimum number to an array so that the sum becomes evenAugust 4, 2017

Given an array, write a program to add the minimum number(should be greater than 0) to the array so that the sum of array becomes… Read More »

## Print ‘K’th least significant bit of a numberAugust 3, 2017

A number N is given. We need to print its ‘K’th Least Significant Bit. Examples: Input : num = 10, k = 4 Output :… Read More »

## Check if two numbers are equal without using comparison operators

Following are not allowed to use 1) Comparison Operators 2) String functions Examples: Input : num1 = 1233, num2 – 1233 Output : Same Input… Read More »

## Set bits in N equals to M in the given range.August 1, 2017

You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i… Read More »

## Check if binary representation of a given number and its complement are anagramJuly 31, 2017

Given a positive number you need to check whether it’s complement and the number are anagrams or not. Examples: Input : a = 4294967295 Output… Read More »

## 1 to n bit numbers with no consecutive 1s in binary representation.July 27, 2017

Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation. Examples: Input… Read More »

## Sum of numbers with exactly 2 bits setJuly 23, 2017

Given a number n. Find sum of all number upto n whose 2 bits are set. Examples: Input : 10 Output : 33 3 +… Read More »