# Category Archives: Bit Magic

## Find smallest number n such that n XOR n+1 equals to given k.September 11, 2017

You are given a positive number k, we need to find a positive integer n, such that XOR of n and n+1 is equal to… Read More »

## Check if a number is power of 8 or notSeptember 10, 2017

Given a number check whether it is power of 8 or not. Examples: Input : n = 64 Output : Yes Input : 75 Output… Read More »

## Check if a number has two adjacent set bits

Given a number you have to check whether there is pair of adjacent set bit or not. Examples: Input : N = 67 Output :… Read More »

## Position of rightmost common bit in two numbersSeptember 8, 2017

Given two non-negative numbers m and n. Find the position of rightmost same bit in the binary representation of the numbers. Examples: Input : m… Read More »

## 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 »