# Category Archives: Bit Magic

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

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 allowed

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 position

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 range

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 C

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 Integer

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 other

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)

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 one

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 set

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) Approach

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.

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 »

## Minimum bitwise operations to convert given a into b.

Given two positive integer a and b you have to change a to b by applying any of the three operations on binary form of… Read More »