Browsing the topic Bit Magic
Write a C program to find the smallest of three integers, without using any of the comparison operators.
Read More »Input: A array arr[] of two elements having value 0 and 1 Output: Make both elements 0.
Read More »Given an integer array of length N (an arbitrarily large number). How to count number of set bits in the array?
Read More »Given a number x, find next number with same number of 1 bits in it’s 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 have modulus instruction at hardware level, in such case the compilers will insert stubs (predefined functions) to perform modulus.
Read More »Write a program to add one to a given number. You are not allowed to use operators like ‘+’, ‘-’, ‘*’, ‘/’, ‘++’, ‘–’ …etc.
Read More »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 »Write a C function that unsets the rightmost set bit of an integer.
Read More »Asked by Ajay 1. A simple method is to take log of the given number on base 4, and if we get an integer then number is power of 4.
Read More »We need not to do anything if a number is positive. We want to change only negative numbers. Since negative numbers are stored in 2′s complement form, to get the absolute value of a negative number we have to toggle bits of the number and add 1 to the result.
Read More »Compute n modulo d without division(/) and modulo(%) operators, where d is a power of 2 number.
Read More »Compute the minimum or maximum of two integers without branching
13 Comments | Filed under Bit MagicOn some rare machines where branching is expensive, the below obvious approach to find minimum can be slow as it uses branching.
Read More »Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.
Read More »Find the two non-repeating elements in an array of repeating elements
8 Comments | Filed under Bit MagicAsked by SG
Read More »Suggested by Dheeraj Question: You are given two numbers A and B. Write a program to count number of bits needed to be flipped to convert A to B.
Read More »Write an efficient program to count number of 1s in binary representation of an integer.
Read More »Method1 – Simple Loop through all the bits of an integer. If a bit at ith position is set in the i/p no. then set the bit at (NO_OF_BITS – 1) – i in o/p. Where NO_OF_BITS is number of bits present in the given number.
Read More »What are these? Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored last.
Read More »Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.
Read More »Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.
Read More »Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer. I/P 18, Binary Representation 010010 O/P 2 I/P 19, Binary Representation 010011 O/P 1
Read More »1. A simple method for this is to simply take the log of the number on base 2 and if you get an integer then number is power of 2.
Read More »We can multiply a number by 7 using bitwise operator. First left shift the number by 3 bits (you will get 8n) then subtract the original numberfrom the shifted number and return the difference (8n – n).
Read More »Parity: Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits. Main idea of the below solution is – Loop while n is not 0 and [...]
Read More »The very first solution that comes to our mind is the one that we learned in school. If sum of digits in a number is multiple of 3 then number is multiple of 3 e.g., for 612 sum of digits is 9 so it’s a multiple of 3. But this solution is not efficient.
Read More »