From WikiChip
Difference between revisions of "binary literal"

m (C++ and C: fixed typo)
Line 27: Line 27:
  
 
== C++ and C ==
 
== C++ and C ==
Binary literals already exist in the C++ and C languages in the form of non-standard compiler extensions<ref>[http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html 6.62 Binary constants using the ‘0b’ prefix]</ref>. They are prefixed with 0b or 0B followed by a series or binary digits. Binary literals have also been proposed<ref>[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3472.pdf C++14 N3472 Binary Literals in the C++ Core Language]</ref> as a core language addition in [[C++14]].
+
Binary literals already exist in the C++ and C languages in the form of non-standard compiler extensions<ref>[http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html 6.62 Binary constants using the ‘0b’ prefix]</ref>. They are prefixed with 0b or 0B followed by a series of binary digits. Binary literals have also been proposed<ref>[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3472.pdf C++14 N3472 Binary Literals in the C++ Core Language]</ref> as a core language addition in [[C++14]].
  
 
== References ==
 
== References ==
 
{{reflist}}
 
{{reflist}}

Revision as of 08:06, 30 December 2013

Binary literal is an integer whose value is represented in the source code using the binary number system. Binary literals are a feature supported in many programming languages such as Java, D and Python as well as a compiler extension in C++ and C. Binary literals typically start with 0b or 0B followed by a series of binary digits.

Java

Starting with Java 7, binary literals are possible to be expressed using the 0b or 0B prefix. For example:

//an octet value
byte b = (byte)0b00010101;

//a 32-bit integer value
int i = 0b001101010110101010001111011101;
int i2 = 0b1110;

Python

Starting with Python 2.6, binary literals support was added via the "0b" and "0B" prefixes.[1]

Lisp

Lisp supports binary literals to represent integers and rational values via the #b prefix, for example:

#b10111

; rational values
#b1110/1111

C++ and C

Binary literals already exist in the C++ and C languages in the form of non-standard compiler extensions[2]. They are prefixed with 0b or 0B followed by a series of binary digits. Binary literals have also been proposed[3] as a core language addition in C++14.

References