• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer Definition  RSS feed

 
Ray Royal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the java code int i4 = 0b101010;

What is the meaning of "b"? A letter seems strange for an integer assignment.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roy, welcome to CodeRanch!

the 0b prefix causes the following digits to be interpreted as binary. 0b101010 is actually 42.

Similarly, prefixing a number with 0x causes it to be interpreted as hexadecimal, and prefixing a number with just 0 causes it to be interpreted as octal. Some examples:

literaldecimal value
1010
0b102
0108
0x1016
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a couple of ways of showing an int, other than straight up decimal.
If it starts '0b' then it's a binary literal.
If it's '0x' then it's hexadecimal.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It is all in the Java® Language Specification (=JLS), though the JLS can be hard to understand. There is probably a section in the Java™ Tutorials which supplies the same information and is probably easier to understand.
You will see that integer literals starting 1 2 3 4 5 6 7 8 9 are decimal, those starting 0 are octal, those starting 0x are hexadecimal and those starting 0b are binary. Binary literals shou‍ld have been there all along, but they were actually introduced in Java7 (about 2009).
There is also 0 as a literal and I think that is decimal but you won't notice much difference if it is a different base
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!