Last active
December 18, 2018 00:31
-
-
Save HaoyangFan/f5e8094f932c64e7935361fad12b55a7 to your computer and use it in GitHub Desktop.
Quick notes on Java Bitwise Operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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