# Category Archives: Bit Magic

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

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 »

## Find two numbers from their sum and XOR

Given the sum and xor of two numbers X and Y s.t. sum and xor , we need to find the numbers minimizing the value… Read More »

## Print all subsequences of a string | Iterative MethodNovember 10, 2017

Given a string s, print all possible subsequences of the given string in an iterative manner. We have already discussed Recursive method to print all… Read More »

## Divisibility by 64 with removal of bits allowedNovember 5, 2017

Given a binary string, we need to check whether that number is divisible by 64 or not after removing of some bits. If yes then… Read More »

## Modify a bit at a given positionNovember 1, 2017

Given a number n, a position p and a binary value b, we need to change the bit at position p in n to value… Read More »

## Count set bits in a rangeOctober 30, 2017

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

## CHAR_BIT in COctober 28, 2017

CHAR_BIT : It is the number of bits in char. These days, almost all architectures use 8 bits per byte (But it is not the… Read More »

## Find One’s Complement of an IntegerOctober 21, 2017

Given an integer n, find the one’s complement of the integer. Examples: Input : n = 5 Output : 2 Input : n = 255… Read More »

## Check if one of the numbers is one’s complement of the otherOctober 17, 2017

Given two non-negative integers a and b. The problem is to check if one of the two numbers is 1’s complement of the other. The… Read More »

## Bits manipulation (Important tactics)October 16, 2017

Prerequisites : Bitwise operators in C, Bitwise Hacks for Competitive Programming, Bit Tricks for Competitive Programming Compute XOR from 1 to n (direct method) :… Read More »

## Unique element in an array where all elements occur k times except oneOctober 3, 2017

Given an array which contains all elements occurring k times, but one occurs only once. Find that unique element. Examples: Input : arr[] = {6,… Read More »

## Find Unique pair in an array with pairs of numbers

Given an array where every element appears twice except a pair (two elements). Find the elements of this unique pair. Examples: Input : 6, 1,… Read More »

## Check whether the number has only first and last bits setSeptember 30, 2017

Given a positive integer n. The problem is to check whether only the first and last bits are set in the binary representation of n.… Read More »

## Check if a number has bits in alternate pattern | Set-2 O(1) ApproachSeptember 29, 2017

Given a positive integer n. The problem is to check whether this integer has an alternate pattern in its binary representation or not. Here alternate… Read More »

## M-th smallest number having k number of set bits.September 24, 2017

Given two non-negative integers m and k. The problem is to find the m-th smallest number having k number of set bits. Constraints: 1 <=… Read More »