Bitwise Operators: ================== Note: ==== 1) Bitwise operations are possible to define with any integer (byte, short, int, long) 2) NO float data is supposed to define bitwise operations. 3) Boolean data also allowed to define for bitwise operations. 4) Character data also possible for bitwise operations. Bitwise And (&) =============== ==> Binary operator Syntax: operand1 & operand2 ex: 10 & 12 10 ==> converted to binary 12 ==> converted to binary ==> bitwise and operation on both data bit by bit from right to left can be defined. class BitwiseOperations{ public static void main(String x[]) { System.out.println(10 & 12); System.out.println(0b11001 & 0b1100); System.out.println(0123 & 0321); System.out.println(0xaf & 0x19); // System.out.println(1.2f & 1.2); System.out.println(true & true); System.out.println(true & false); System.out.println(false & true); System.out.println(false & false); // System.out.println(true & 100); System.out.println('a' & 'b'); System.out.println('A' & 10); System.out.println(100 & 'B'); } } ===================================================== Bitwise Or (|) ============== ==> Binary Operator Syntax: operand1 | operand2 class BitwiseOperations{ public static void main(String x[]) { System.out.println(10 | 12); System.out.println(true | true); System.out.println(true | false); System.out.println(false | true); System.out.println(false | false); System.out.println('a' | 'b'); } } ================================================================ Bitwise XOR Operator (^): ========================= ==> Binary operator. Syntax: operand1 ^ operand a b a^b =================== 0 0 0 0 1 1 1 0 1 1 1 0 ==> for same inputs (0 & 0, 1 & 1), output ==> '0' for different (altered) inputs (0 & 1, 1 & 0), output ==> '1' class BitwiseOperations{ public static void main(String x[]) { System.out.println(10 ^ 12); System.out.println(true ^ true); System.out.println(true ^ false); System.out.println(false ^ true); System.out.println(false ^ false); System.out.println('a' ^ 'b'); } } =============================================== Bitwise Complement (~): ====================== + ==> - - ==> + ==> Unary Operator Syntax: ~operand ==> 2's complement of the number. ==> 2's complement ==> 1's complement + binary of '1' ==> 1's complement ==> in a data: all 1's ==> 0 all 0's ==> 1 ex: 110011001 1's ==> 001100110 Note: ==== Binary System: negative numbers: 7 ==> 0111 if data has started with '0' ==> +eve if data has started with '1' ==> -eve ex: -7 ==> 10111 Note: ====== 1) While the calculation of 2's complement, if the carry (1) is present, we should rounded-off the carry. means: carry should add to the lsb in a result. convert that result ==> decimal and put '-' sign before the result. 2) While the calculation of 2's complement, if there is no carry (1) is present: then: for that result, we should calculate the 2's complement again. after that, for the result: convert into decimal put - sign add -1 to the above result class BitwiseOperations{ public static void main(String x[]) { System.out.println(~0); System.out.println(~10); System.out.println(~1); System.out.println(~-6); } } ================================================== Left Shift operator (<<): ========================= ==> binary operator Syntax: data << n.times class BitwiseOperations{ public static void main(String x[]) { System.out.println(10 << 1); System.out.println(20 << 1); System.out.println(40 << 1); System.out.println(10 << 3); System.out.println(-10 << 3); } } ============================================= Right Shift Operator (>>): ========================== ==> Binary operator Syntax: data >> n.times class BitwiseOperations{ public static void main(String x[]) { System.out.println(10 >> 1); System.out.println(20 >> 1); System.out.println(40 >>1); System.out.println(10 >> 3); System.out.println(-10 >> 3); } } ============================================ Unsigned Right Shift Operator (>>>) ==================================== ==> Binary operator Syntax: data >>> n.times Unsigned ==> numbers are +eve (0) ==> also called as "zero filling right shift operator" class BitwiseOperations{ public static void main(String x[]) { System.out.println(10 >> 2); System.out.println(10 >>> 2); System.out.println(-10 >>> 1); // System.out.println(10 <<< 2); } }