0% found this document useful (0 votes)
200 views11 pages

SET - 13 (Based On Bit Manipulation) PDF

The document contains descriptions of several bit manipulation problems and tasks to write C functions or programs to solve them. Some of the problems included are: finding the next power of 2 of a given number, finding the parity of an integer, checking if a number is a power of two, and finding the position of the rightmost set bit in a number's binary representation. It also includes tasks to find non-repeating elements in an array, check for integer overflow, reverse bits of a number, and more bit manipulation and number problems.

Uploaded by

MUKUL MANDLOI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views11 pages

SET - 13 (Based On Bit Manipulation) PDF

The document contains descriptions of several bit manipulation problems and tasks to write C functions or programs to solve them. Some of the problems included are: finding the next power of 2 of a given number, finding the parity of an integer, checking if a number is a power of two, and finding the position of the rightmost set bit in a number's binary representation. It also includes tasks to find non-repeating elements in an array, check for integer overflow, reverse bits of a number, and more bit manipulation and number problems.

Uploaded by

MUKUL MANDLOI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SET- 13

(Based on Bit Manipulation)

Next Power of 2
Write a function that, for a given no n, finds a number p which is greater than or equal to n and is a power
of 2.
IP 5

OP 8

IP 17

OP 32

IP 32

OP 32

Write a C program to find the parity of an unsigned integer


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.

Write one line C function to find whether a no is power of two

Position of rightmost set bit


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

Find the Number Occurring Odd Number of Times


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.
Example:
I/P = [1, 2, 3, 2, 3, 1, 3]
O/P = 3
Check for Integer Overflow
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.

Write an Efficient C Program to Reverse Bits of a Number

Count set bits in an integer


Write an efficient program to count number of 1s in binary representation of an integer.

Count number of bits to be flipped to convert A to B


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.

A = 1001001

B = 0010101

No of bits need to flipped = set bit count in a_xor_b i.e. 4

Find the two non-repeating elements in an array of repeating


elements
Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n
numbers are occurring twice and remaining two have occurred once). Find those two numbers in the
most efficient way.

Rotate bits of a number


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.
In left rotation, the bits that fall off at left end are put back at right end.
In right rotation, the bits that fall off at right end are put back at left end.
Example:
Let n is stored using 8 bits. Left rotation of n = 11100101 by 3 makes n = 00101111 (Left shifted by 3 and
first 3 bits are put back in last ). If n is stored using 16 bits or 32 bits then left rotation of n
(000…11100101) becomes 00..0011100101000.
Right rotation of n = 11100101 by 3 makes n = 10111100 (Right shifted by 3 and last 3 bits are put back
in first ) if n is stored using 8 bits. If n is stored using 16 bits or 32 bits then right rotation of n
(000…11100101) by 3 becomes 101000..0011100.

Compute the minimum or maximum of two integers without


branching
On some rare machines where branching is expensive, the below obvious approach to find minimum can
be slow as it uses branching.

Compute the integer absolute value (abs) without branching


Turn off the rightmost set bit
Write a C function that unsets the rightmost set bit of an integer.
Examples:

Input: 12 (00...01100)

Output: 8 (00...01000)

Input: 7 (00...00111)

Output: 6 (00...00110)

Add 1 to a given number


Write a program to add one to a given number. You are not allowed to use operators like ‘+’, ‘-‘, ‘*’, ‘/’,
‘++’, ‘–‘ …etc.
Examples:
Input: 12
Output: 13
Input: 6
Output: 7

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 representation is 1100 (excluding leading zeros on 32 bit
machine). It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (10001 2).

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?

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) It is guaranteed that one element is 0 but we do not know its position.
2) We can’t say about another element it can be 0 or 1.
3) We can only complement array elements, no other operation like and, or, multi, division, …. etc.
4) We can’t use if, else and loop constructs.
5) Obviously, we can’t directly assign 0 to array elements.

Smallest of three integers without comparison operators


Write a C program to find the smallest of three integers, without using any of the comparison operators.

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 (+, ++, –, -, .. etc).
Swap bits in a given number
Given a number x and two positions (from right side) in binary representation of x, write a function that
swaps n bits at given two positions and returns the result. It is also given that the two sets of bits do not
overlap.

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 = 3

Output: 4

Input: n = 6

Output: 9

Input: n = 7

Output: 12

Input: n = 8

Output: 13

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 return true -1 and +100, and should return false for -100
and -200. The function should not use any of the arithmetic operators.

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 is O(n) and O(1) extra space.
Examples:
Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}

Output: 2

Write your own strcmp that ignores cases


Write a modified strcmp function which ignores cases and returns -1 if s1 < s2, 0 if s1 = s2, else returns 1.
For example, your strcmp should consider "GeeksforGeeks" and "geeksforgeeks" as same string.
Add two bit strings
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. For example, if string 1 is “1100011” and second string 2 is “10”,
then the function should return “1100101”.

Swap all odd and even bits


Given an unsigned integer, swap all odd bits with even bits. For example, if the given number is 23
(00010111), it should be converted to 43 (00101011). Every even position bit is swapped with adjacent bit
on right side (even position bits are highlighted in binary representation of 23), and every odd position bit
is swapped with adjacent on left side.

Find position of the only set bit


Given a number having only one ‘1’ and all other ’0’s in its binary representation, find position of the only
set bit.

How to swap two numbers without using a temporary variable?


Given two variables, x and y, swap two variables without using a third variable.

How to turn off a particular bit in a number?


Difficulty Level: Rookie
Given a number n and a value k, turn of the k’th bit in n.
Examples:

Input: n = 15, k = 1

Output: 14

Input: n = 15, k = 2

Output: 13

Input: n = 15, k = 3

Output: 11

Input: n = 15, k = 4

Output: 7
Input: n = 15, k >= 5

Output: 15

Swap two nibbles in a byte


A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte.
Given a byte, swap the two nibbles in it. For example 100 is be represented as 01100100 in a byte (or 8
bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in
decimal.
Check if binary representation of a number is palindrome
Given an integer ‘x’, write a C function that returns true if binary representation of x is palindrome else
return false.
For example a numbers with binary representation as 10..01 is palindrome and number with binary
representation as 10..00 is not palindrome.

Calculate square of a number without using *, / and pow()


Given an integer n, calculate square of a number without using *, / and pow().
Examples:

Input: n = 5

Output: 25

Input: 7
Output: 49

Input: n = 12
Output: 144

Subtract two numbers without using arithmetic operators


Write a function subtract(x, y) that returns x-y where x and y are integers. The function should not use any
of the arithmetic operators (+, ++, –, -, .. etc).

Find n’th Magic Number


A magic number is defined as a number which can be expressed as a power of 5 or sum of unique
powers of 5. First few magic numbers are 5, 25, 30(5 + 25), 125, 130(125 + 5), ….
Write a function to find the n’th Magic number.
Example:

Input: n = 2 Output: 25

Input: n = 5 Output: 130

Check if a given number is sparse or not


A number is said to be a sparse number if in binary representation of the number no two or more
consecutive bits are set. Write a function to check if a given number is Sparse or not.
Example:

Input: x = 72 Output: true

Explanation: Binary representation of 72 is 01001000.


There are no two consecutive 1's in binary representation

Input: x = 12
Output: false
Explanation: Binary representation of 12 is 1100.
Sum of bit differences among all pairs
Given an integer array of n integers, find sum of bit differences in all pairs that can be formed from array
elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations
of x and y.
For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 ( first and last
bits differ in two numbers).
Examples:

Input: arr[] = {1, 2}

Output: 4

All pairs in array are (1, 1), (1, 2)

(2, 1), (2, 2)

Sum of bit differences = 0 + 2 +

2 + 0

= 4

Input: arr[] = {1, 3, 5}

Output: 8

All pairs in array are (1, 1), (1, 3), (1, 5)

(3, 1), (3, 3) (3, 5),

(5, 1), (5, 3), (5, 5)

Sum of bit differences = 0 + 1 + 1 +

1 + 0 + 2 +

1 + 2 + 0

= 8

Find Next Sparse Number


A number is Sparse if there are no two adjacent 1s in its binary representation. For example 5 (binary
representation: 101) is sparse, but 6 (binary representation: 110) is not sparse.
Given a number x, find the smallest Sparse number which greater than or equal to x.
Examples:

Input: x = 6

Output: Next Sparse Number is 8

Input: x = 4 Output: Next Sparse Number is 4

Input: x = 38 Output: Next Sparse Number is 40

Input: x = 44 Output: Next Sparse Number is 64


Find the maximum subarray XOR in a given array
Given an array of integers. find the maximum XOR subarray value in given array. Expected time
complexity O(n).
Examples:

Input: arr[] = {1, 2, 3, 4}

Output: 7

The subarray {3, 4} has maximum XOR value

Input: arr[] = {8, 1, 2, 12, 7, 6}

Output: 15

The subarray {1, 2, 12} has maximum XOR value

Input: arr[] = {4, 6}


Output: 6

The subarray {6} has maximum XOR value

Sum of Bitwise And of all pairs in a given array


Given an array “arr[0..n-1]” of integers, calculate sum of “arr[i] & arr[j]” for all the pairs in the given where i
< j. Here & is bitwise AND operator. Expected time complexity is O(n).

Examples:

Input: arr[] = {5, 10, 15}

Output: 15

Required Value = (5 & 10) + (5 & 15) + (10 & 15)

= 0 + 5 + 10

= 15

Input: arr[] = {1, 2, 3, 4}

Output: 3

Required Value = (1 & 2) + (1 & 3) + (1 & 4) +

(2 & 3) + (2 & 4) + (3 & 4)

= 0 + 1 + 0 + 2 + 0 + 0

= 3

Given a set, find XOR of the XOR’s of all subsets.


The question is to find XOR of the XOR’s of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2},
{3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every
subset result.
Find the maximum subset XOR of a given set
Given an set of positive integers. find the maximum XOR subset value in the given set. Expected time
complexity O(n).
Examples:

Input: set[] = {2, 4, 5}

Output: 7

The subset {2, 5} has maximum XOR value

Input: set[] = {9, 8, 5}

Output: 13

The subset {8, 5} has maximum XOR value

Input: set[] = {8, 1, 2, 12, 7, 6}

Output: 15

The subset {1, 2, 12} has maximum XOR value

Input: set[] = {4, 6}

Output: 6

The subset {6} has maximum XOR value

Count strings with consecutive 1’s


Given a number n, count number of n length strings with consecutive 1’s in them.
Examples:

Input : n = 2

Output : 1

There are 4 strings of length 2, the

strings are 00, 01, 10 and 11. Only the

string 11 has consecutive 1's.

Input : n = 3

Output : 3

There are 8 strings of length 3, the

strings are 000, 001, 010, 011, 100,

101, 110 and 111. The strings with

consecutive 1's are 011, 110 and 111.

Input : n = 5

Output : 19
Check if a number is Bleak
A number ‘n’ is called Bleak if it cannot be represented as sum of a positive number x and set bit count in
x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x.
Examples :

Input : n = 3

Output : false

3 is not Bleak as it can be represented

as 2 + countSetBits(2).

Input : n = 4

Output : true

4 is t Bleak as it cannot be represented

as sum of a number x and countSetBits(x)

for any number x.

Copy set bits in a range


Given two numbers x and y, and a range [l, r] where 1 <= l, r <= 32. The task is consider set bits of y in
range [l, r] and set these bits in x also. Examples :

Input : x = 10, y = 13, l = 2, r = 3

Output : x = 14

Binary representation of 10 is 1010 and


that of y is 1101. There is one set bit
in y at 3’rd position (in given range).
After we copy this bit to x, x becomes 1110
which is binary representation of 14.

Input : x = 8, y = 7, l = 1, r = 2
Output : x = 11

Write an Efficient Method to Check if a Number is Multiple of 3

Efficient way to multiply with 7

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


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 %, /, *.
Examples:
Input: 2
Output: 7
Input: 5
Output: 17 (Ignore the digits after decimal point)

Generate n-bit Gray Codes


Given a number n, generate bit patterns from 0 to 2^n-1 such that successive patterns differ by one bit.
Examples:
Following is 2-bit sequence (n = 2)

00 01 11 10

Following is 3-bit sequence (n = 3)

000 001 011 010 110 111 101 100

And Following is 4-bit sequence (n = 4)

0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111

1110 1010 1011 1001 1000

Check if a number is multiple of 9 using bitwise operators


Given a number n, write a function that returns true if n is divisible by 9, else false.

Calculate 7n/8 without using division and multiplication operators


Given an integer, write a function that calculates ⌈7n/8⌉ (ceiling of 7n/8) without using division and
multiplication operators.

!!! All The Best !!!

You might also like