Given two bit sequences as strings, write a function to return the addition of the two sequences. Bit strings can be of different lengths also.… Read More

# Category Archives: Bit Magic

In C/C++ there is only one right shift operator ‘>>’ which should be used only for positive integers or unsigned integers. Use of right shift… Read More

Write a modified strcmp function which ignores cases and returns -1 if s1 < s2, 0 if s1 = s2, else returns 1. For example,… Read More

Write a program to print Binary representation of a given number. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Source:… Read More

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

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

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

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 in… Read More

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

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

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 z.Method… Read More

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

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

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 representation… Read More

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