Skip to content

Instantly share code, notes, and snippets.

@pithyless
Created March 24, 2014 10:50
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save pithyless/9738125 to your computer and use it in GitHub Desktop.
Save pithyless/9738125 to your computer and use it in GitHub Desktop.
Ruby Integer::MAX and Integer::MIN
class Integer
N_BYTES = [42].pack('i').size
N_BITS = N_BYTES * 16
MAX = 2 ** (N_BITS - 2) - 1
MIN = -MAX - 1
end
p Integer::MAX #=> 4611686018427387903
p Integer::MAX.class #=> Fixnum
p (Integer::MAX + 1).class #=> Bignum
@brauliobo
Copy link

thanks!

@darktef
Copy link

darktef commented May 20, 2017

thank you!

@prio101
Copy link

prio101 commented Oct 22, 2017

Thanks

@aks
Copy link

aks commented Apr 3, 2018

The number of bits in a byte is 8, not 16. So, shouldn't line 3 read N_BITS = N_BYTES * 8?
which makes the rest incorrect.

The maximum unsigned integer, with 4 bytes, is 2**32 - 1 => 4294967295
The maximum signed integer, with 4 bytes, is 2**31 - 1 => 2147483647

@joslinm
Copy link

joslinm commented Nov 26, 2019

right on! 🏄‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment