Bit Magic
1. Find first set bit
2. Rightmost different bit
3. Check whether K-th bit is set or not
4. Toggle bits given range
5. Set kth bit
6. Power of 2
7. Bit Difference
8. Rotate Bits
9. Swap all odd and even bits
10. Count total set bits
11. Longest Consecutive 1’s
12. Sparse Number
13. Alone in a couple
14. Maximum subset XOR
Theory - about bit
Decimal number system = is a number system where base is 10
Binary number system = is a number system where base is 2
To find the digits in decimal numbers, output = [1, 2, 3]
def Find_digits(num):
digits = []
if num == 0: digits.append(0)
while num:
digits.insert(0, num % 10)
num = num// 10
print(digits)
Find_digits(123)
Similarly, To find the digits in Binary, make 10 to 2, output = [1, 1, 1, 1, 0, 1, 1]
def Find_digits(num):
digits = []
if num == 0: digits.append(0)
while num:
digits.insert(0, num % 2)
num = num// 2
print(digits)
Find_digits(123)
To convert binary to decimal
def binary_to_decimal(num):
res = 0
i = 1
while num:
res += num%10 * i
num = num//10
i = i * 2
print(res)
binary_to_decimal(1111011)
Python bit operators:
~ complement, basically this operator, adds one number then changes the sign
Operation => (num + 1) then followed by sign change
12 => -13
-1 =. 0
-2 => 1
& operator, this operator does bitwise and operation and give the result
12 & 13 => 12
Because 12 => 00001100
13 => 00001101
& => 00001100 => 12
| logical or operator does bitwise logical or operation
12 | 13 => 13
^ is XOR operator
0 0 => 0
0 1 => 1
1 0 => 1
1 1 => 0
!2 ^ 13 => 1
Left shift << in this operation we gain bits
10 << 2 => 40
1010 => 101000
Right shift >> in this operation we loose bits
10 >> 2 => 2
1010 => 10
Q1 Find the first set bit
class Solution:
def getFirstSetBit(self,n):
i = 1
while n:
if n % 2: return i
n = n//2
i += 1
return 0
Q2 Rightmost different bit
class Solution:
def posOfRightMostDiffBit(self,m,n):
i = 1
while m or n:
if m % 2 != n % 2: return i
m = m//2
n = n//2
i += 1
return -1
Q3 check whether Kth bit is set or not:
class Solution:
def checkKthBit(self, n,k):
i = 0
while i != k:
n = n//2
i += 1
return n % 2
Q4 Toggle bits given in a rane
class Solution:
def toggleBits(self, N , L , R):
res = []
i = 1
while N:
if L <= i and i <= R:
if N % 2 == 1:
res.append(0)
else: res.append(1)
else:
res.append(N % 2)
N = N //2
i += 1
ans = 0
k = 1
for i in res:
ans += i * k
k = k * 2
return ans
Q5 Set a bit
class Solution:
def setKthBit(self, N, K):
res = []
i = 0
while N:
if i == K:
res.append(1)
else: res.append(N % 2)
N = N//2
i += 1
k = 1
ans = 0
for i in res:
ans += i * k
k = k * 2
return ans
Q6 Power of 2
class Solution:
def isPowerofTwo(self,n):
c = 0
while n:
if n % 2 == 1: c += 1
n = n//2
if c != 1: return False
return True
Better solution but don’t know why it works
class Solution:
def isPowerofTwo(self,n):
if n == 0: return False
return not n & n -1
Q7 Bit difference
class Solution:
def countBitsFlip(self,a,b):
num = a ^ b
c = 0
while num:
if num % 2: c+= 1
num = num // 2
return c
Q18 Rotate Bit
class Solution:
def rotate(self, N, D):
res = [0] * 16
i = 0
while N:
res[i] = N % 2
N = N //2
i += 1
D = D%16
a = check_num(res[D:] + res[:D])
b = check_num(res[-D:] + res[:-D])
return [b, a]
def check_num(arr):
num = 0
k = 1
for i in arr:
num += i * k
k *= 2
return num
Better solution
class Solution:
def rotate(self, N, D):
D = D % 16
t = 1 << 16
return [(N << D | N >> (16 - D)) % t, \
(N >> D | N << (16 - D)) % t]