Bitwise operators in Java can manipulate individual bits of numeric values and are used for operations on binary indexed trees. The main bitwise operators are:
1) Bitwise OR (|) returns a 1 in the result if either or both bits of the inputs are 1.
2) Bitwise AND (&) returns a 1 in the result only if both bits of the inputs are 1.
3) Bitwise XOR (^) returns a 1 in the result if the bits of the inputs are different.
4) Bitwise complement (~) inverts all the bits of the input.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
56 views
Bitwise Operators in Java
Bitwise operators in Java can manipulate individual bits of numeric values and are used for operations on binary indexed trees. The main bitwise operators are:
1) Bitwise OR (|) returns a 1 in the result if either or both bits of the inputs are 1.
2) Bitwise AND (&) returns a 1 in the result only if both bits of the inputs are 1.
3) Bitwise XOR (^) returns a 1 in the result if the bits of the inputs are different.
4) Bitwise complement (~) inverts all the bits of the input.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Bitwise operators in Java
Bitwise operators are used to perform manipulation of individual bits of a number.
They can be used with any of the integral types (char, short, int, etc). They are used when performing update and query operations of Binary indexed tree. 1. Bitwise OR (|) – This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e, if either of the bits is 1, it gives 1, else it gives 0. For example, 2. a = 5 = 0101 (In Binary) 3. b = 7 = 0111 (In Binary) 4. 5. Bitwise OR Operation of 5 and 7 6. 0101 7. | 0111 8. ________ 0111 = 7 (In decimal) 9. Bitwise AND (&) – This operator is binary operator, denoted by ‘&’. It returns bit by bit AND of input values, i.e, if both bits are 1, it gives 1, else it gives 0. For example,
10. a = 5 = 0101 (In Binary)
11. b = 7 = 0111 (In Binary) 12. 13. Bitwise AND Operation of 5 and 7 14. 0101 15. & 0111 16. ________ 0101 = 5 (In decimal) 17. Bitwise XOR (^) – This operator is binary operator, denoted by ‘^’. It returns bit by bit XOR of input values, i.e, if corresponding bits are different, it gives 1, else it gives 0. For example, 18. a = 5 = 0101 (In Binary) 19. b = 7 = 0111 (In Binary) 20. 21. Bitwise XOR Operation of 5 and 7 22. 0101 23. ^ 0111 24. ________ 0010 = 2 (In decimal) 25. Bitwise Complement (~) – This operator is unary operator, denoted by ‘~’. It returns the one’s compliment representation of the input value, i.e, with all bits inversed, means it makes every 0 to 1, and every 1 to 0. For example, 26. a = 5 = 0101 (In Binary) 27. 28. Bitwise Compliment Operation of 5 29. 30. ~ 0101 31. ________ 1010 = 10 (In decimal) Note – Compiler will give 2’s complement of that number, i.e., 2’s compliment of 10 will be -6.