# Category: Binary

## 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. Get great deals at Amazon

## Generate Binary Numbers Between 1 to N using Queue

Given a positive number N, efficiently generate binary numbers between 1 to N using queue data structure and in linear time.

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

## Multiply 16-bit integers using 8-bit multiplier

Given two 16-bit positive values stored in 32-bit integer variables, find the product using the 8-bit multiply operator that that takes two 8-bit numbers and returns a 16-bit value.

## Find the minimum or maximum of two integers without using branching

Given two integers, find their minimum and maximum without using branching.

## Count Set Bits using Lookup Table

Given an integer, count set bits in it.

## Compute Parity of a Number using Lookup Table

In this post, we will see how to compute parity of a number using lookup table. The parity refers to the number of 1’s in a given binary number.   Odd parity (encoded as 1) means there are an odd number of 1’s and even parity (encoded as 0) means that there are an even …

## Brian Kernighanâ€™s Algorithm to count set bits in an integer

Given an integer, count its set bits.

## Conditionally negate a value without branching

In this post, we will see how to conditionally negate a value without branching.   The expression ((flag^(flag-1))*n) negate n when the flag is false and the expression ((n^-flag)+flag) negate n when the flag is true.   How does it work?   1. For (flag ^ (flag – 1)) * n if flag = 0, …