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



1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
# File 'lib/setfu.rb', line 1232

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



1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
# File 'lib/setfu.rb', line 1247

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



1267
1268
1269
1270
# File 'lib/setfu.rb', line 1267

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