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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
What exactly are you trying to do? From the Fixnum documentation:
"A Fixnum holds Integer values that can be represented in a native
machine word (minus 1 bit). If any operation on a Fixnum exceeds this
range, the value is automatically converted to a Bignum.
Fixnum objects have immediate value. This means that when they are
assigned or passed as parameters, the actual object is passed, rather
than a reference to that object. Assignment does not alias Fixnum
objects. There is effectively only one Fixnum object instance for any
given integer value, so, for example, you cannot add a singleton
method to a Fixnum."
Is that the answer you are looking for?
On 6/10/06, Guest <ifxa3a@yahoo.com> 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--
Posted via http://www.ruby-forum.com/\.
--
- Simen
Keep watching the Ruby Quiz...
James Edward Gray II
On Jun 10, 2006, at 4:16 AM, 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
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