From: "Suraj N. Kurapati" Date: 2006-06-11T05:39:50+09:00 Subject: Re: limiting Ruby Numbers -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Guest wrote: > is there any ways to limit ruby numbers up to certain bytes? all i've > read is that ruby numbers are limited to host's free memory Use a bit mask to truncate the integer: irb(main):011:0> ((2 ** 32) - 1) => 4294967295 irb(main):012:0> ((2 ** 32) - 1) & 0xFF => 255 irb(main):013:0> ((2 ** 32) - 1) & 0xFFFF => 65535 irb(main):014:0> ((2 ** 32) - 1) & 0xFFFFFF => 16777215 irb(main):015:0> ((2 ** 32) - 1) & 0xFFFFFFFF => 4294967295 For negative integers, be sure to preserve the sign by converting the result back into two's complement form: irb(main):021:0> -1 => -1 irb(main):022:0> -1 & 0xFF => 255 irb(main):023:0> ~(-1 & 0xFF) + 1 => -255 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFEiy4HmV9O7RYnKMcRAmgSAJ4tCb3MTAGidDfh4lUniVDgJ2coMACeO4a0 x/h1y/QYbKE8VaEzMiyhdMU= =YkLw -----END PGP SIGNATURE-----