Back to Scala3

Binary Integer Literals

docs/_docs/reference/other-new-features/binary-literals.md

3.8.3610 B
Original Source

A new syntax for integer literals has been added, it is now possible to do the following:

scala
val bitmask = 0b0010_0000 // equivalent to 32, 0x20

Binary integer literals behave similarly to hex integer literals (0x...), for example:

  • Both 0b... and 0B... are allowed
  • 0b/0B on its own is disallowed, possible alternatives: 0, 0b0, 0B0
  • Only 0 and 1 are allowed after the b (b/B)
  • Underscores _ are allowed anywhere between digits, and are ignored: 0b__1 == 0b1

Note: This change has been backported to Scala 2.13.13, it is therefore not technically a changed feature