class BitUtil
Public Class Methods
new()
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 27 def initialize @masks = [ 0xffff, 0xff ] @rightTable = [ 0 ] @leftTable = [ 0 ] (1..255).each do |val| @rightTable << rightOneBit(val) @leftTable << leftOneBit(val) end end
Public Instance Methods
bitGreaterThanOrEqualTo(startOffset, v1, v2)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 48 def bitGreaterThanOrEqualTo(startOffset, v1, v2) map = (1 << (startOffset - 1)) while (startOffset < 64) do if (((v1 & map) != 0) && ((v2 & map) != 0)) then return startOffset else startOffset += 1 map = map << 1 end end end
leftBit(n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 43 def leftBit(n) shiftCount, n = shiftToByteLeft(n) @leftTable[n & 0xff] + shiftCount end
leftMostBitToRightOf(bitNumber, n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 60 def leftMostBitToRightOf(bitNumber, n) mask = getMask(bitNumber + 1) return leftBit(n & mask) end
rightBit(n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 38 def rightBit(n) shiftCount, n = shiftToByteRight(n) @rightTable[n & 0xff] + shiftCount end
Private Instance Methods
getMask(bitNumber)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 67 def getMask(bitNumber) # assume 64 bit numbers 0xffffffffffffffff >> bitNumber end
leftOneBit(n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 116 def leftOneBit(n) mask = 1 << 7 result = 8 while ((n & mask) == 0) do mask = mask >> 1 result -= 1 end return result end
rightOneBit(n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 106 def rightOneBit(n) mask = 1 result = 1 while ((n & mask) == 0) do mask = mask << 1 result += 1 end return result end
shiftToByteLeft(n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 89 def shiftToByteLeft(n) shiftCount = 0 if ((n & 0xffffffff00000000) != 0) then n = n >> 32 shiftCount += 32 end if ((n & 0xffff0000) != 0) then n = n >> 16 shiftCount += 16 end if ((n & 0xff00) != 0) then n = n >> 8 shiftCount += 8 end return shiftCount, n end
shiftToByteRight(n)
click to toggle source
# File lib/visitor/numbering_visitor.rb, line 72 def shiftToByteRight(n) shiftCount = 0 if ((n & 0xffffffff) == 0) then n = n >> 32 shiftCount += 32 end if ((n & 0xffff) == 0) then n = n >> 16 shiftCount += 16 end if ((n & 0xff) == 0) then n = n >> 8 shiftCount += 8 end return shiftCount, n end