What is a Binary Literal?

Go To StackoverFlow.com

0

Can someone give a clear explanation of what Binary Literal is? What is the difference between binary literals, hexadecimal and binary numbers, strings? What are they used for?

2012-04-04 23:28
by Vincent
Literals: http://cpp.comsci.us/etymology/literals.html. A binary literal would most likely be a string of the form "0b11011011" - Robert Harvey 2012-04-04 23:32


2

They're used for expressing a number using bits.

0b0010010101001
2012-04-04 23:30
by Ignacio Vazquez-Abrams


2

Because sometimes it's easier to convey the intent of a value in binary. This applies to base 16 as well. They're all numbers when it comes down to it, but if I want to assign a flag with multiple bits set, something like this seems clearer than the alternative(s).

flags = 0b110101
2012-04-04 23:31
by Ed S.
Even that may be less clear (though far less verbose) than using a bitwise-OR of named flag constants. :- - Platinum Azure 2012-04-04 23:35
@PlatinumAzure: Oh it certainly would be. I can't think of a lot of uses for binary literals in practice... this is what I cam up with : - Ed S. 2012-04-04 23:48
It's a great example and I'll give you +1. I was just providing some color commentary :- - Platinum Azure 2012-04-05 01:13


1

Literals: http://cpp.comsci.us/etymology/literals.html.

Literal constants (often referred to as literals or constants) are invariants whose values are implied by their representations

Just as a hexadecimal literal is a string of the form "0xABCD", a binary literal is a string of the form "0b11011011". They can be distinguished from each other by checking the first two characters.

http://docs.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html

2012-04-04 23:33
by Robert Harvey
Ads