Class: Integer

Inherits:
Object show all
Defined in:
lib/setfu.rb,
lib/setfu.rb

Overview

for older rubbies … slow but works … >2.1.0 supports this and is much faster no need to improve as 1.9x is no longer supported

Instance Method Summary collapse

Instance Method Details

#bit_lengthObject



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/setfu.rb', line 1050

def bit_length
  pos = 0
  n = self
  loop do
    break if n == 0
    break if n == -1
    n >>= 1
    pos += 1
  end
  return pos
end

#lsb_bit_positionObject



1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'lib/setfu.rb', line 1065

def lsb_bit_position
  return nil if zero?
  pos = 0
  n = self
  while (n & 0xffffffffffffffff) == 0
    n >>= 64
    pos += 64
  end
  while (n & 0xff) == 0
    n >>= 8
    pos += 8
  end
  mask = 1
  loop do
    break if mask == (n & mask)
    mask <<= 1
    pos += 1
  end
  return pos
end

#msb_bit_positionObject



1085
1086
1087
1088
# File 'lib/setfu.rb', line 1085

def msb_bit_position
  t = bit_length
  return t.zero? ? nil : t-1
end