Literals/Integer: Difference between revisions

From Rosetta Code
Content added Content deleted
(C / fix python to lang python (right?!))
(ruby + perl)
Line 20: Line 20:
C has no way of specifying integers in binary (if there's something like <tt>0b...</tt>, it is not
C has no way of specifying integers in binary (if there's something like <tt>0b...</tt>, it is not
standard)
standard)


=={{header|Perl}}==

<lang perl>
print "true\n" if ( (727 == 0x2d7) &&
(727 == 01327) &&
(727 == 0b1011010111) );
</lang>




Line 29: Line 38:
>>>
>>>
</lang>
</lang>


=={{header|Ruby}}==

(This is an interactive ruby session)

<pre>
irb(main):001:0> 727 == 0b1011010111
=> true
irb(main):002:0> 727 == 0x2d7
=> true
irb(main):003:0> 727 == 01327
=> true
</pre>

Revision as of 23:27, 1 February 2009

Task
Literals/Integer
You are encouraged to solve this task according to the task description, using any language you may know.

Some programming languages have ways of expressing integer literals in bases other than the normal base ten.

Show how integer literals can be expressed in as many bases as your language allows.

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.

C

Leading 0 means octal, 0x or 0X means hexadecimal. Otherwise, it is just decimal.

<lang c>#include <stdio.h>

int main() {

 printf("%s\n",

( (727 == 0x2d7) && (727 == 01327) ) ? "true" : "false"); }</lang>

C has no way of specifying integers in binary (if there's something like 0b..., it is not standard)


Perl

<lang perl> print "true\n" if ( (727 == 0x2d7) && (727 == 01327) && (727 == 0b1011010111) ); </lang>


Python

<lang python>>>> # Bin(leading 0b), Oct(leading 0), Dec, Hex(leading 0x or 0X), in order: >>> 0b1011010111 == 01327 == 727 == 0x2d7 True >>> </lang>


Ruby

(This is an interactive ruby session)

irb(main):001:0> 727 == 0b1011010111
=> true
irb(main):002:0> 727 == 0x2d7
=> true
irb(main):003:0> 727 == 01327
=> true