# Category Archives: Bit Magic

## Bitmasking and Dynamic Programming | Set-2 (TSP)June 23, 2017

In this post, we will be using our knowledge of dynamic programming and Bitmasking technique to solve one of the famous NP-hard problem “Travelling Salesman… Read More »

## Next greater integer having one more number of set bitsJune 11, 2017

Given a positive integer ‘n’ having ‘x’ number of set bits in its binary representation. The problem is to find the next greater integer(smallest integer… Read More »

## Set the K-th bit of a given number

Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or… Read More »

## Check if two numbers are bit rotations of each other or not

Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y <… Read More »

## XOR of all subarray XORs | Set 2June 7, 2017

Given an array of integers, we need to get total XOR of all subarray XORs where subarray XOR can be obtained by XORing all elements… Read More »

## Bit Tricks for Competitive Programming

In competitive programming or in general some problems seems difficult but can be solved very easily with little bit magic. We have discussed some tricks… Read More »

## Closest (or Next) smaller and greater numbers with same number of set bitsJune 5, 2017

Given a positive integer n, print the next smallest and the previous largest number that have the same number of 1 bits in their binary… Read More »

## Toggle all the bits of a number except k-th bit.June 1, 2017

Given a positive (or unsigned) integer n, write a function to toggle all the bits except k-th bit. Here value of k starts from 0… Read More »

## Toggle case of a string using Bitwise operators

Given a string, write a function that returns toggle case of a string using the bitwise operators in place. In ASCII codes character ‘A’ is… Read More »

## Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++May 31, 2017

Given a string, write a function that converts it either from lower to upper case or from upper to lower case using the bitwise operators… Read More »

## Determine if a string has all Unique CharactersMay 24, 2017

Given a string, Determine if the string has all unique characters. Examples: Input : abcd10jk Output : true Input : hutg9mnd!nk9 Output : false Recommended:… Read More »

## Reverse an array without using subtract sign ‘-‘ anywhere in the codeMay 21, 2017

Given an array, the task is to reverse the array without using subtract sign ‘-‘ anywhere in your code. It is not tough to reverse… Read More »

## Count numbers whose sum with x is equal to XOR with xMay 15, 2017

Given a integer ‘x’, find the number of values of ‘a’ satisfying the following conditions: 0 <= a <= x a XOR x = a… Read More »

## Inserting m into n such that m starts at bit j and ends at bit i.May 11, 2017

We are given two numbers n and m, and two-bit positions, i and j. Insert bits of m into n starting from j to i.… Read More »

## Maximum XOR value of a pair from a rangeMay 10, 2017

Given a range [L, R], we need to find two integers in this range such that their XOR is maximum among all possible choices of… Read More »