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?

- 95,083
- 20
- 220
- 214

- 16,086
- 18
- 67
- 73
-
1Literals: http://cpp.comsci.us/etymology/literals.html. A binary literal would most likely be a string of the form "0b11011011". – Robert Harvey Apr 04 '12 at 23:32
3 Answers
They're used for expressing a number using bits.
0b0010010101001

- 776,304
- 153
- 1,341
- 1,358
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

- 122,712
- 22
- 185
- 265
-
Even that may be less clear (though far less verbose) than using a bitwise-OR of named flag constants. :-) – Platinum Azure Apr 04 '12 at 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. Apr 04 '12 at 23:48
-
It's a great example and I'll give you +1. I was just providing some color commentary :-) – Platinum Azure Apr 05 '12 at 01:13
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

- 178,213
- 47
- 333
- 501