L18 Bit Manip2
L18 Bit Manip2
2
Show the memory map to find your m/c
Endian
void show_mem_rep(char *start, int n)
{ int i;
for (i = 0; i < n; i++) printf(" %x", start[i]);
}
int main()
{ int i = 0x12345678;
printf("\nByte wise Memory (asc order of addresses)
contents for a = x12345678 are \n");
show_mem_rep((char *)&i, sizeof(i));
3
Practice Questions
• P1: Convert an integer stored in Little Endian to Big-Endian
representation and vice versa. So A = 0x12345678 will get printed
as 0x78563412 for different Endian representation.
• P2: You have an integer and you can flip exactly one bit from 0 to
1. Find the length of longest sequence of 1s you could create in its
binary representation.
• Sol: Store an array having alternatively length of consecutive
0s and 1s from R to L (can take from L to R also). For ex
11011101111 would be {0,4,1,3,1,2} Here 0 is length of
consecutive 0s on LSB side, and 4 is consecutive 1s and so on.
• Now zeroseq is at even indices (0,4,1,3,1,2).
• If zeroseq is 1 then left+right+1 is longest for this flip; if
zeroseq > 1 , 1+max of left/right. (if zeroseq == 0, max of
left/right, but should we think of this?)
4
Searching using Bit Operations
• Q1: Find the missing no in an array of unique n-1 elements in
the range of 1..n
• Sol : XOR
• Any advantage of using XOR compared to solving it using : sum
of array vs n*(n+1)/2??
5
Searching using Bit Operations
• Q2: An array has n+2 numbers in the range of 1..n and exactly
two numbers X and Y are repeated. Find X as well as Y.
• Sol : XOR comes to mind. But how to find X separately?
6
Mixing of bits
• Q3: Given two integers M and N (max 32 bit) and given two bit
positions i and j, insert all bits of N into M such that bits of N
start at index j and end at index i. For ex.
M = 1000 0000 0000 0000 (16 bit), N = 10011 i = 2, j = 6
RESULT = 1000 0000 0100 1100
7
Interview Question (slightly difficult): Next
Number
• Q4: Given a +ive int N, print next smallest number having same
number of 1s in the binary representation. For ex
N = 12 0000 1100 Find next smallest no > 12 having two 1s
• Brute Force: start with N+1, count 1s, then N+2 and so on
• correct answer is 17 0001 0001
• Logically: next number means some 0 needs to become 1 and some
bit of 1 [MUST BE less significant than the set/toggled bit ] needs to
become 0.
8
Interview Question (slightly difficult): Next
Number
• Intuition: Start thinking for some initial odd and even numbers. Some
logic will start coming
• if we have …01 on the rightmost side, it can become …10 (If N =
1(0001) ans =2 (0010); If N = 5 (0101) res = (0110)
• If we have …010 on rightmost, it will become …100 (n = 2 means
ans = 4, N = 4 means ans = 8, n = 10 (1010) means ans = 12
• If we have …11?
• Can we think of …011 as …101?(N=3 means ans = 5, N= 7 ans = 11)
• Always think of some larger values for each such patterns before
accepting your intuition.
• For N =12 it fails, Why?
9
Interview Question (slightly difficult): Next
Number
• What’s wrong: We did not think of bits on the right of the patterns
(always thought of rightmost 0 to become 1 and neighboring 1 to
become 0 to balance count of 1s).
• 01100 was thought as 10100, but 10001 is answer
• …01 1111 1000 ? Ofcourse …1followed by 5 1s towards LSB are the
answer, hence flipping one rightmost 0 is ok, but count 1s on its right
as C, and rightmost C bits should be made as 1.
10
11
Practice problems: Optional
• https://www.hackerrank.com/domains/algorithms?filters%5Bsubdo
mains%5D%5B%5D=bit-manipulation
• Interview questions practice:
https://www.interviewbit.com/courses/programming/topi
cs/bit-manipulation/
• GL Mcdowell Book’s Bit manipulation Chapter
• Nakariakov’s chapter of Bit Manipulation
12