示例#1
0
 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
示例#2
0
 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
示例#3
0
 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)