def getNextBiggerNumber(self, num): #first find the non trailing 0 oneCounter = 0 pivot = -1 bitCounter = -1 foundOne = False curNum = num while curNum > 0: bitCounter += 1 if curNum % 2 == 0: if foundOne: pivot = bitCounter break else: oneCounter += 1 foundOne = True curNum = curNum // 2 if pivot == -1: return None #now we need to: #set the bit at pivot, reset all the trailing 0, add back oneCounter-1 "1" temp = Generic.setBit(num, pivot) #print(Generic.DecToBinaryString(temp)) temp = Generic.clearBitFromStart(temp, pivot - 1) #print(Generic.DecToBinaryString(temp)) setNum = (1 << oneCounter - 1) - 1 #print(Generic.DecToBinaryString(setNum)) temp = temp | setNum #print(Generic.DecToBinaryString(temp)) return temp
def getPrevSmallerNumber(self, num): #first find the non trailing 1 oneCounter = 0 pivot = -1 bitCounter = -1 foundZero = False curNum = num while curNum > 0: bitCounter += 1 if curNum % 2 == 0: foundZero = True else: if foundZero: pivot = bitCounter break else: oneCounter += 1 curNum = curNum // 2 if pivot == -1: return None #now we need to: #reset the bit at pivot till start, add back oneCounter+1 "1" temp = Generic.clearBitFromStart(num, pivot) print(Generic.DecToBinaryString(temp)) setNum = (1 << oneCounter + 1) - 1 print(Generic.DecToBinaryString(setNum)) temp = temp | setNum print(Generic.DecToBinaryString(temp)) return temp
def insertion(self, a, b, i, j): if len(b) > j - i + 1: raise IndexError #Create mask of 0 from j -> i, rest should be 1 -> ~ (1 from i-j) numA = Generic.BinaryStringToDec(a) numB = Generic.BinaryStringToDec(b) #Clear bit j->i of num A mask = ~(Generic.clearBitFromStart(((1 << j + 1) - 1), i - 1)) numA = numA & mask #shit left num b i bit numB = numB << i return Generic.DecToBinaryString(numA | numB)
import BitManipulation.Generic as Generic class Solution: def flipBit(self, num): data = {} oneCounter = 0 prevIdx = -1 curBit = -1 while num > 0: curBit += 1 temp = num % 2 if temp == 0: data[curBit] = oneCounter + 1 if prevIdx in data: data[prevIdx] += oneCounter prevIdx = curBit oneCounter = 0 else: oneCounter += 1 num = num // 2 if prevIdx in data: data[prevIdx] += oneCounter return max(data.values()) if __name__ == "__main__": bin = "11011100111" num = 1775 A = Solution() print(A.flipBit(Generic.BinaryStringToDec(bin)))
import BitManipulation.Generic as Generic class Solution: def pairwiseSwap(self, num): #Assume number is of type integer 32 bits onlyEven = 0xAAAAAAAA onlyOdd = 0x55555555 #calculate even and odd bit number even = num & onlyEven odd = num & onlyOdd #shift right even + shiftleft odd even = even >> 1 odd = odd << 1 return even | odd if __name__ == '__main__': bin = "110110101101100" print(" " + bin) num = Generic.BinaryStringToDec(bin) A = Solution() res = A.pairwiseSwap(num) print(Generic.DecToBinaryString(res))
while curNum > 0: bitCounter += 1 if curNum % 2 == 0: foundZero = True else: if foundZero: pivot = bitCounter break else: oneCounter += 1 curNum = curNum // 2 if pivot == -1: return None #now we need to: #reset the bit at pivot till start, add back oneCounter+1 "1" temp = Generic.clearBitFromStart(num, pivot) print(Generic.DecToBinaryString(temp)) setNum = (1 << oneCounter + 1) - 1 print(Generic.DecToBinaryString(setNum)) temp = temp | setNum print(Generic.DecToBinaryString(temp)) return temp if __name__ == '__main__': A = Solution() binNum = "11011001111011" num = Generic.BinaryStringToDec(binNum) print(num) print(A.getPrevSmallerNumber(num))
#Find number of bit needed to be flipped to turn a -> b import BitManipulation.Generic as Generic class Solution: def conversion(self,a ,b): res = 0 while a > 0 and b > 0: if a%2 != b%2: res += 1 a = a//2 b = b//2 while a>0: if a%2 == 1: res += 1 a = a//2 while b > 0: if b%2 == 1: res += 1 b = b//2 return res if __name__ == '__main__': a = 100 b = 15 print(Generic.DecToBinaryString(a)) print(Generic.DecToBinaryString(b)) A = Solution() print(A.conversion(b,a))