Skip to content

Instantly share code, notes, and snippets.

@mlem
Last active December 17, 2015 14:49
Show Gist options
  • Select an option

  • Save mlem/5627499 to your computer and use it in GitHub Desktop.

Select an option

Save mlem/5627499 to your computer and use it in GitHub Desktop.
Operations with binary literals in Java 7
import spock.lang.Specification
class BinarySpec extends Specification {
def "binary operations in java 7"() {
expect:
0b1000_0000 == 128 // binary literal
0b1000_0000 + 0b1 == 129 // binary addition
1025 + 0b100 == 1029 // addition of decimal and binary
1025 + 0b100 == 0b100_0000_0101 // comparing result to binary
1024 << 2 == 4096 // left shift by two bits
0b1000_1111 << 2 == 0b10_0011_1100 // left shift with binary
(0b1000_0000 ^ 0b0100_0000) == 0b1100_0000 // XOR
(0b1000_0000 | 0b0100_0000) == 0b1100_0000 // OR
(0b1000_0000 & 0b1111_1111) == 0b1000_0000 // AND
((~0b1000_0000) & 0b1111_1111) == 0b0111_1111 // NEGATION with a bitmask
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment