# Tag: Bit Hacks

## Reverse Bits of an integer using lookup table

Given an integer, reverse its bits using binary operators and lookup table in O(1) time. Get great deals at Amazon

## Swap Adjacent Bits of a Number

Given an integer, swap adjacent bits of it. In other words, swap bits present at even positions with those present in odd positions.

## Find two odd occurring element in an array without using any extra space

Given an array of integers, duplicates appear in it even number of times except two elements which appears odd number of times. Find both odd appearing element without using any extra memory.

## Swap two bits at given position in an integer

Given an integer, swap two bits at given positions in binary representation of it.

## Find odd occurring element in an array in single traversal

Given an array of integers, duplicates are present in it in such a way that all duplicates appear even number of times except one which appears odd number of times. Find that odd appearing element in linear time and without using any extra memory.

## Check if given number is power of 4 or not

Given a number, check if it is power of four or not.

## Check if given number is power of 8 or not

Given a number, check if it is power of 8 or not.

## Find two duplicate elements in an limited range array (using XOR)

Given an array of integers with all its elements between 1 to n with the exception of two elements which occur twice. Find two duplicate elements without using any extra memory.

## Find missing number and duplicate elements in an array

Given an limited range array of integers of size n with all its elements between 1 to n with the exception of one element which occur twice and one number which is missing from the list. Find missing number and the duplicate element in linear time and without using any extra memory.   For example, …

## Swap individual bits at given position in an integer

Given an integer, swap consecutive b bits starting from given positions in binary representation of the integer. The bits to be swapped should not overlap with each other.

## Round up to the previous power of 2

Given a number n, find previous power of 2. If n itself is a power of 2, return n.

## Round up to the next highest power of 2

Given a number n, find next highest power of 2. If n itself is a power of 2, return n.