Skip to content

Instantly share code, notes, and snippets.

@HaoyangFan
Last active December 18, 2018 00:31
Show Gist options
  • Select an option

  • Save HaoyangFan/f5e8094f932c64e7935361fad12b55a7 to your computer and use it in GitHub Desktop.

Select an option

Save HaoyangFan/f5e8094f932c64e7935361fad12b55a7 to your computer and use it in GitHub Desktop.
Quick notes on Java Bitwise Operator
/*
When applied to boolean values, the & and | operators yield a boolean value.
These operators are similar to the && and || operators,
except that the & and |operators are not evaluated in “short circuit” fashion—that is,
both arguments are evaluated before the result is computed.
*/
public class BitwiseOps {
public static void main(String[] args) {
/* Bitwise operators: & (and), | (or), ^ (xor), ~(not) */
System.out.println(true & false); // "AND" : false
System.out.println(true | false); // "OR" : true
System.out.println(true ^ true); // "XOR" : false
// System.out.println(~true); // error: bad operand type boolean for unary operator '~'
/* Bitwise shift operators: >> (sign extended right shift), >>> (zero fill right shift), << (left shift) */
byte b = (byte)0b10000000;
System.out.println(b);
System.out.println(b >> 1); // sign extension
System.out.println(b >>> 1); // zero extension
System.out.println(b << 1); // zero extension
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment