Literals/Integer: Difference between revisions

Content deleted Content added
No edit summary
Line 5: Line 5:
Note: this should '''not''' involve the calling of any functions/methods but should be interpreted by the compiler or interpreter as an integer written to a given base.
Note: this should '''not''' involve the calling of any functions/methods but should be interpreted by the compiler or interpreter as an integer written to a given base.


Also show any other ways of expressing literals, e.g. for different types of integers.


=={{header|Ada}}==
=={{header|Ada}}==
Line 159: Line 160:
Java has no way of specifying integers in binary.
Java has no way of specifying integers in binary.


You may also specify a <tt>long</tt> literal by adding an <tt>l</tt> or <tt>L</tt> to the end (ex: <tt>long a = 727L</tt>), though this is not required because <tt>int</tt>s coerce into <tt>long</tt>s.
You may also specify a <tt>long</tt> literal by adding an <tt>l</tt> or <tt>L</tt> (the latter form is preferred as the former looks like a "1") to the end (ex: <tt>long a = 574298540721727L</tt>), and this is required for numbers that are too large to be expressed as an <tt>int</tt>.


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 180: Line 181:
- : bool = true
- : bool = true
# 727 = 0x2d7;;
# 727 = 0x2d7;;
- : bool = true
# 12345 = 12_345 (* underscores are ignored; useful for keeping track of places *);;
- : bool = true
- : bool = true
</lang>
</lang>

Literals for the other built-in integer types:
* <tt>727l</tt> - int32
* <tt>727L</tt> - int64
* <tt>727n</tt> - nativeint


=={{header|Perl}}==
=={{header|Perl}}==
Line 188: Line 196:
print "true\n" if ( 727 == 0x2d7 &&
print "true\n" if ( 727 == 0x2d7 &&
727 == 01327 &&
727 == 01327 &&
727 == 0b1011010111 );
727 == 0b1011010111 &&
12345 == 12_345 # underscores are ignored; useful for keeping track of places
);
</lang>
</lang>


Line 220: Line 230:
>>>
>>>
</lang>
</lang>

In Python 2.x you may also specify a <tt>long</tt> literal by adding an <tt>l</tt> or <tt>L</tt> (the latter form is preferred as the former looks like a "1") to the end (ex: <tt>574298540721727L</tt>), but this is optional, as integer literals that are too large for an <tt>int</tt> will be interpreted as a <tt>long</tt>.


=={{header|Ruby}}==
=={{header|Ruby}}==


(This is an interactive ruby session)
(This is an interactive irb session)


<pre>
<pre>
Line 231: Line 243:
=> true
=> true
irb(main):003:0> 727 == 01327
irb(main):003:0> 727 == 01327
=> true
irb(main):001:0> 12345 == 12_345 # underscores are ignored; useful for keeping track of places
=> true
=> true
</pre>
</pre>