0% found this document useful (0 votes)
14 views4 pages

Java Bitwise Operators Explained

The document discusses bitwise operators in Java. It provides examples of how bitwise operators like &, |, ^, ~, <<, >>, and >>> operate on binary representations of integers. It also summarizes the common bitwise operators and provides code examples showing how to use them in Java programs. Additionally, it introduces hexadecimal numbering and conventions for writing hex numbers in Java code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Java Bitwise Operators Explained

The document discusses bitwise operators in Java. It provides examples of how bitwise operators like &, |, ^, ~, <<, >>, and >>> operate on binary representations of integers. It also summarizes the common bitwise operators and provides code examples showing how to use them in Java programs. Additionally, it introduces hexadecimal numbering and conventions for writing hex numbers in Java code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Bitwise Operators:

Java defines several bitwise operators, which can be applied to the


integer types, long, int, short, char, and byte.

Bitwise operator works on bits and performs bit-by-bit operation.


Assume if a = 60; and b = 13; now in binary format they will be as
follows:

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

The following table lists the bitwise operators:

Assume integer variable A holds 60 and variable B holds 13 then:

[Link] Operator and Description

1 & (bitwise and)

Binary AND Operator copies a bit to the result if it exists in both


operands.

Example: (A & B) will give 12 which is 0000 1100

2 | (bitwise or)
Binary OR Operator copies a bit if it exists in either operand.
Example: (A | B) will give 61 which is 0011 1101

3 ^ (bitwise XOR)
Binary XOR Operator copies the bit if it is set in one operand but not
both.

Example: (A ^ B) will give 49 which is 0011 0001

4 ~ (bitwise compliment)
Binary Ones Complement Operator is unary and has the effect of
'flipping' bits.

Example: (~A ) will give -61 which is 1100 0011 in 2's complement form
due to a signed binary number.

5 << (left shift)


Binary Left Shift Operator. The left operands value is moved left by the
number of bits specified by the right operand

Example: A << 2 will give 240 which is 1111 0000

6 >> (right shift)


Binary Right Shift Operator. The left operands value is moved right by
the number of bits specified by the right operand.

Example: A >> 2 will give 15 which is 1111

7 >>> (zero fill right shift)


Shift right zero fill operator. The left operands value is moved right by
the number of bits specified by the right operand and shifted values are
filled up with zeros.

Example: A >>>2 will give 15 which is 0000 1111


public class Test {

public static void main(String args[]) {

int a = 60; /* 60 = 0011 1100 */

int b = 13; /* 13 = 0000 1101 */

int c = 0;

c = a & b; /* 12 = 0000 1100 */

[Link]("a & b = " + c );

c = a | b; /* 61 = 0011 1101 */

[Link]("a | b = " + c );

c = a ^ b; /* 49 = 0011 0001 */

[Link]("a ^ b = " + c );

c = ~a; /*-61 = 1100 0011 */

[Link]("~a = " + c );

c = a << 2; /* 240 = 1111 0000 */

[Link]("a << 2 = " + c );

c = a >> 2; /* 15 = 1111 */

[Link]("a >> 2 = " + c );

c = a >>> 2; /* 15 = 0000 1111 */

[Link]("a >>> 2 = " + c );

}
Hexadecimal numbers:
Hexadecimal (or "hex" for short) is a numbering system which works
similarly to our regular decimal system, but where a single digit can take a
value of 0-15 rather than 0-9. The extra digits are represented by the letters
A-F, as shown in the table below.

They look the same as the decimal numbers up to 9, but then there are
the letters ("A',"B","C","D","E","F") in place of the decimal numbers 10 to
15.

So a single Hexadecimal digit can show 16 different values instead of the


normal 10 like this:

Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F

How to write a hex number in Java code


Sometimes it is convenient to represent a number in hex in your Java code.
There are generally two conventions for writing hex numbers. The first is to
write h after the hex number. For example, 400h is the equivalent of 1024
decimal.

The second convention is to put 0x before the number, e.g. 0x400. This is
the convenion used by Java:
int noBytes = 0x400; // = 1024

Common questions

Powered by AI

In Java, the left shift operator (<<) shifts the bits of a number to the left by a specified number of positions, filling the least significant bits with zeros. For example, with the value 60 (0011 1100 in binary) shifted left by 2 positions, it becomes 240 (1111 0000 in binary). The zero fill right shift (>>>) operator shifts bits to the right, padding the leftmost bits with zeros instead of the sign bit, making it unsigned. For the same 60 shifted right by 2 (>>>) results in 15 (0000 1111 in binary), as the empty positions are filled with zeros .

The binary right shift (>>) operator shifts bits to the right, preserving the sign bit (the leftmost bit), thus maintaining the number's sign. For example, shifting the integer 60 (0011 1100 in binary) by 2 using >> results in 15 (0000 1111 in binary). The zero fill right shift (>>>) operator shifts bits to the right, but the leftmost bits are filled with zeros regardless of the sign, which can alter the sign for negative numbers but remains the same for positive numbers like 60. For 60 >>> 2, the result is the same, 15 (0000 1111 in binary), as the preserved bits remain 0 .

Hexadecimal and binary systems are both base systems used to represent numerical values, with binary being base 2 and hexadecimal being base 16. One hexadecimal digit can be exactly represented by four binary digits (bits), making conversion between them straightforward. For instance, the binary number 1111 0000 corresponds to the hexadecimal number F0. Java developers might prefer hexadecimal because it provides a more compact and human-readable form compared to binary for the same numerical value, especially useful when dealing with memory addresses or bit-level manipulation .

Using right shift operators on negative numbers in Java has significant implications due to how negative numbers are represented with two's complement binary form. The arithmetic right shift operator (>>) preserves the sign bit, resulting in a behavior that mimics division by two but rounds toward negative infinity. For instance, -8 (11111000 in an 8-bit system) shifted right by 2 results in -2 (11111110), maintaining the negative sign. However, the zero fill right shift (>>>) does not preserve the sign, filling with zeros regardless. Thus, a negative number shifted using >>> can become a large positive number due to left-side zero padding, which may not be the intended outcome .

In Java, hexadecimal numbers can be written using two conventions: either by appending an 'h' at the end of the number, although uncommon, or by prefixing the number with '0x'. For example, 0x400 is the equivalent of the decimal number 1024. This is because in hexadecimal, '400' converts to 4 * 16^2, which equals 1024 .

The NOT bitwise operator (~) is unary and flips the bits of an integer, effectively inverting each bit: 1s become 0s and 0s become 1s. For the integer 60, which is 0011 1100 in binary, the NOT operator results in -61. The operation flips all bits of 60 to 1100 0011, which in two's complement form represents -61 .

In Java, using the NOT operator (~) on a signed integer flips its bits, affecting its representation through two's complement notation, which is used for encoding signed integers. In two's complement, the leftmost bit indicates the sign (0 for positive, 1 for negative). Taking 60 (0011 1100 in binary) as an example, applying ~ results in 1100 0011, which is interpreted as -61 in two's complement format. The inversion results in a negative number due to the first bit switch from 0 to 1 .

When the integer 240 (1111 0000 in binary) is left-shifted by 2 places in Java using the << operator, the operation shifts all bits two places to the left, introducing two zeros in the least significant spots. The two leftmost bits shift out of the visible range for a typical 8-bit integer, but in Java's int type, which uses 32 bits, they are simply removed. Therefore, the result is 960 (0011 1100 0000 in binary), as spaces created on the right side are filled with zeros .

Bitwise operators manipulate integer values by performing bit-by-bit operations on the binary representations of those values. The OR operator (|) copies a bit if it exists in either operand. For the integers 60 (0011 1100 in binary) and 13 (0000 1101 in binary), using the OR operator results in 61, which is 0011 1101 in binary. This is because whenever there is a 1 in either corresponding bit of the operands, the result bit is set to 1 .

The XOR (^) operator differs from the OR (|) operator in that XOR copies a bit if it is set in one operand but not both, whereas OR copies a bit if it exists in either operand. When applying XOR to the integers 60 (0011 1100 in binary) and 13 (0000 1101 in binary), the result is 49 (0011 0001 in binary). This is because the XOR operation results in a 1 only if the corresponding bits of the operands differ .

You might also like