Bitwise Operations: Working on individual bits

Write code to perform the basic operations on individual bits of a number (rather than the entire number): setBit – Set a particular bit. resetBit – Reset a particular bit. toggleBit – Toggle a particular bit. getBit – return the value (0 or 1) stored at a particular bit position.

Find numbers with more than k-bits in their binary representations

Given an array of unsigned integers, print all the numbers that have more than k bits set in their binary representation. For example, if k = 2, and input array is: int arr[] = {2, 1, 15, 9, 7, 14, 32, 127}; Then the output should be: 15 7 14 127 Because all of these […]

Maximum XOR value of two elements

Given an array of numbers find the maximum XOR value of two numbers in the array. Input Array = {12, 15, 5, 1, 7, 9, 8, 6, 10, 13}; Output = 15 (XOR of 5 and 10) 5 = 0101 10 = 1010 ——— XOR = 1111

Compute x^n (x to the power n)

Write a C function to compute xn which uses minimum number of multiplications. unsigned int power(double x, unsigned int n)

Next greater power of 2

Given an unsigned int n, find the integer greater than or equal to n which is a power of 2. For example: Input Output —– —— 6 8 12 16 64 64

Making both elements of an array zero

Given an Array of two elements one of which is 0, the other is either 0 or 1. You have to make both the elements of array 0. But following are the constraints:

Check if number is a power of 2

How will you check if a number if a power of two or not? For example: 4, 8, 1024 etc are powers of two. But 6, 40, 95 etc are not