# Category Archives: Bit Magic

## Find the element that appears once

Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. Expected time complexity… Read More »

## Detect if two integers have opposite signs

Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. For example, the function should… Read More »

## Count total set bits in all numbers from 1 to n

Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples: Input: n… Read More »

## Josephus problem | Set 1 (A O(n) Solution)

In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem. Following is the problem statement: There are n people standing… Read More »

## Swap bits in a given number

Given a number x and two positions (from the right side) in the binary representation of x, write a function that swaps n bits at… Read More »

## Add two numbers without using arithmetic operators

Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, ..… Read More »

## Smallest of three integers without comparison operators

Write a program to find the smallest of three integers, without using any of the comparison operators. Let 3 input numbers be x, y and… Read More »

## A Boolean Array Puzzle

Input: A array arr[] of two elements having value 0 and 1 Output: Make both elements 0. Specifications: Following are the specifications to follow. 1)… Read More »

## Program to count number of set bits in an (big) array

Given an integer array of length N (an arbitrarily large number). How to count number of set bits in the array? The simple approach would be, create… Read More »

## Next higher number with same number of set bits

Given a number x, find next number with same number of 1 bits in it’s binary representation. For example, consider x = 12, whose binary… Read More »

## Optimization Techniques | Set 1 (Modulus)

Modulus operator is costly. The modulus operator (%) in various languages is costly operation. Ultimately every operator/operation must result in processor instructions. Some processors won’t… Read More »

## Add 1 to a given number

Write a program to add one to a given number. The use of operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–‘ …etc are not allowed.… Read More »

## Multiply a given Integer with 3.5

Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /,… Read More »

## Turn off the rightmost set bit

Write a program that unsets the rightmost set bit of an integer. Examples : Input: 12 (00…01100) Output: 8 (00…01000) Input: 7 (00…00111) Output: 6… Read More »

## Find whether a given number is a power of 4 or not

Given an integer n, find whether it is a power of 4 or not. Example : Input : 16 Output : 16 is a power… Read More »