How java handles the results of bitwise operators - java

I trying to know to know how java finds the result is -ve or +ve for Bitwise operations?
int x=-5;
int y=8;
System.out.println(x&y); //8
System.out.println(x|y); //-5
x->1 0 1 1 (2's complement)
y->1 0 0 0
x & y -> 1 0 0 0 ->8
x | y -> 1 0 1 1 ->-5(2's complement)
How java knows 1 0 1 1 is -5 ?
why doesn't it directly give o/p as 1 0 1 1's decimal equivalent 11 ?
Does it apply 2's complement on every result ?
I have seen the Assembly Code . It is IAND and IOR instructions.

You are running the bitwise operators on 32-bit integers. So, the number "8" really has a lot of zeros in front, while "-5" has a lot of ones:
8 -> 0...01000
-5 -> 1...11011
So, Java does not need to "know" anything about the outcome or operands of the bitwiese operations. The "8" is a 32-bit number that starts with a 0, so it is positive. The "-5" is a 32-bit number that starts with a "1", so it is negative.
So, the answer to your question
Does it apply 2's complement on every result ?
is: Yes, since all integers are signed number in Java, using 2's complement.

Related

Unable to understand Bitwise & operator in java

int number = 3;
System.out.println(number & 1 << 2);
Given this snippet where I am performing bitwise AND to number and then left shifting by 2, Why is the result 0 and not 4 (0100)?
Make it (number & 1) << 2 because what you are doing means shift 1 << 2 times and then & that with number.
First, as multiple people have already mentioned, the & operator's precedence is smaller than <<'s. So your code would look like this: number & (1 << 2). Let's imagine your code looked like this instead: (number % 1) << 2. Now, what does this do?
First, let's talk about the & operator. What does it do? In short, it will apply an AND gate to each bit of the two given numbers, recording the result of the gate in a new number:
a 0 0 1 1
b 1 1 1 0
result 0 0 1 0
An AND gate works in the following way: the result of this gate is 1 only when both inputs are 1, otherwise, the output of the gate is 0.
In your case, you have the following:
a ...number
b 0 0 0 0 0 0 0 1
Since each bit but the first one of b is 0, the result will be all 0s, except for the first bit, which will be whatever the first bit of number was (note that a & 1 is virtually equivalent to a % 2).
The shift operator now will shift the only remaining bit to the left 2 bits, which will virtually multiply it by 4.
So, for example, if number was 3, 3 & 1 would be 1 (the first bit of 3 is 1), and then 1 would be shifted over 2 bits, so the result would be 4.
In fact, the expression (number & 1) << 2 will produce only two values:
4, when the number is odd (its first bit is 1)
0, when the number is even (its first bit is 0)

Need help understanding this line

thank you in advance for this basic question.
I am going through a tutorial and I see this line.
int a = (n & 8) / 8
This is supposed to identify whether the fourth bit from the right is a binary representation of 0 or 1. I understand the concept of bits etc, but I do not understand what mathematical equation (if any) this represents.
Would anyone care to explain how this would be written in a mathematical equation? Also, please let me know if i am missing anything else in my understanding of this line. Thank you.
The expression ( n & 8 )
does Logical And of n with 1000 binary.
So that gets the 4th bit from right.
then dividing that by 8, shifts the value right 3 binary places. I.e. it moves the 4th bit to the rightmost place.
That is more clearly expressed as " >> 3"
So your overall expression would be something like:
(n AND 1000 ) >> 3
And that leaves the 4th bit of N in a temporary variable, as bit 0 (rightmost bit).
All the other bits will be zero because of the AND.
8 in decimal is 1000 in binary
so if you do bitwise AND with any number
n & 8
it will stay 8 only if the 4th bit is 1 and
if you divide it by 8 again it will return 1, zero otherwise
For example
for 9 (1001)
9 & 8
would be
1001
& 1000
------
1000
Now for the case where forth bit is 0
for 7 (0111)
7 & 8
would be
0111
& 1000
-----
0000
int a = (n & 8) / 8;
The n & 8 applys a logical AND mask to the 4th bit of n;
n: 11001010 // example value
8: 00001000
result: 00001000
Dividing that number by 8 brings the result to the lowest bit :
result: 00000001
Dividing a number by 2^n shifts the numbers n bits to the right (in the same way that multiplying by 2^n shifts bits to the left).
The result is assigned to variable a, which now contains 0 or 1, depending on the value of the 4th bit.
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
then a&b is also an integer which is further divided by 8 in your example.

Java Operators with Numbers

How it works the & Operator with numbers?
For example:
x = 8 & 4;
and the answer is 0.
How can i find the answer without using java or any other program?
You can find the answer without using Java converting the numbers into binary format:
4 = 0100
8 = 1000
If you perform an AND operation on each bit, you get:
0 & 1 = 0
1 & 0 = 0
0 & 0 = 0
0 & 0 = 0
That's why it's zero.
Java has three kinds of "AND" operators - the logical one, which is &&, a bitwise one, which is &, and a & on booleans, which does not short-circuit. Java compiler can distinguish between the kinds of operators by examining the type of the operands.
Bitwise operator takes binary representations of its operand, performs "AND" on each bit, and returns the result as a number.
For 4 and 8, bitwise "and" is zero, because they do not have 1 bits in common:
00000100 -- 8
00001000 -- 4
--------
& 00000000 -- 0
For other numbers, say, 22 and 5, the result would be different:
00010110 -- 22
00000101 -- 5
--------
& 00000100 -- 4
& is a bitwise and operator ... the binary values of 8 and 4 are
8 = 1000
4 = 0100
if you know how AND operator works then you know that
1 & 0 = 0;
so after a bitwise AND, there will be all 0

Understanding bitwise operations

This code segment:
(x >>> 3) & ((1 << 5) - 1)
apparently results in a 5-bit integer with bits 3 - 7 of x.
How would you go about understanding this?
Let's look at ((1 << 5) - 1) first.
1 << 5 is equal to 100000 in binary.
When we subtract 1, we're left with 11111, a binary number of five 1s.
Now, it's important to understand that a & 0b11111 is an operation that keeps only the 5 least significant bits of a. Recall that the & of two bits is 1 if and only if both of the bits are 1. Any bits in a above the 5th bit, therefore, will become 0, since bit & 0 == 0. Moreover, all of the bits from bit 1 to bit 5 will retain their original value, since bit & 1 == bit (0 & 1 == 0 and 1 & 1 == 1).
Now, because we shift the bits of x in x >>> 3 down by 3, losing the three least significant bits of x, we are applying the process above to bits 4 to 8 (starting at index 1). Hence, the result of the operation retains only those bits (if we say the first bit is bit 0, then that would indeed be bit 3 to bit 7, as you've stated).
Let's take an example: 1234. In binary, that's 10011010010. So, we start with the shift by 3:
10011010010 >>> 3 = 10011010
Essentially we just trim off the last 3 bits. Now we can perform the & operation:
10011010
& 00011111
--------
00011010
So, our final result is 11010. As you can see, the result is as expected:
bits | 1 0 0 1 1 0 1 0 0 1 0
index | 10 9 8 7 6 5 4 3 2 1 0
^-------^
(x >>> 3)
Shifts x right 3 bits logically, i.e. not sign-extending at the left. The lower-order 3 bits are lost. (This is equivalent to an unsigned division by 8.)
1 << 5
Shifts 1 left 5 bits, i.e. multiplies it by 32, yielding 0b00000000000000000000000000100000.
-1
Subtracts one from that, giving 31, or 0b00000000000000000000000000011111.
&
ANDs these together, yielding only the lower-order 5 bits of the result of x >>> 3, in other words bits 3..7 of the original x.
"How would you go about understanding this?".
I assume that you are actually asking how you should go about understanding it. (As distinct from someone just explaining it to you ...)
The way to understand it is to "hand execute" it.
Get a piece of paper and a pencil.
Based on your understanding of how Java operator precedence works, figure out the order in which the operations will be performed.
Based on your understanding of each operator, write the input patterns of bits on the piece of paper and "hand execute" each operation ... in the correct order.
If you do this a few times with a few values of x, you should get to understand why this expression gives you a 5 bit number.
If you repeat this exercise for a few other examples, you should get to the point where you don't need to go through the tedious process of working it out with a pencil and paper.
I see that #arshajii has essentially done this for you for this example. But I think you will get a deeper understanding if you do / repeat the work for yourself.
One thing to remember about integer and bitwise operations in Java is that the operations are always performed using 32 or 64 bit operations ... even if the operands are 8 or 16 bit. Another thing to remember (though it is not relevant here) is that the right hand operand of a shift operator is chopped to 5 or 6 bits, depending on whether this is a 32 or 64 bit operation.

Java | operator with integers;

I do program with Java for about one year, but still found something I do not know.
How does:
new Font(FontFamily.TIMES_ROMAN, 12, 1 | 4);
How | does work with integers?
Thank You
P.S. I googled a lot.
The | operator calculates the "bit-wise OR" of its operands. To understand it you have to convert the operands to binary: it produces a "0" bit if the bit is not set in either numbers, and a "1" bit if it is set in either.
With your numbers, the result of 4|1 is 5 because:
4 = 100
1 = 001
4|1 = 101 = 5
The bit-wise OR operator is related to the "bit-wise AND" operator &, which produces a "0" if the bit is not set in one of the numbers and a "1" bit if it is set in both.
Since these operators work on the bit-wise representation of their arguments they can be hard to understand when you're used to working on decimal (base 10) numbers. The following relation holds, which makes it easy to derive the result of one when you have the other:
a + b = (a|b) + (a&b)
It is a bitwise OR operator , operates on one or more bit patterns or binary numerals at the level of their individual bits.
The bitwise ^ operator performs a bitwise exclusive OR operation.
OR bitwise operation will return 1 if any of operand is 1 and zero only if both operands are zeros.
You can get the complete description in the JLS 15.22.1.
0|0 = 0
0|1 = 1
1|0 = 1
1|1 = 1
Hence in your case , the operands are 1 and 4 . Converting them into binary (only the last 4 digits) will be 0100 and 0001 respectively. Apply the | now bit by bit :
0 1 0 0
0 0 0 1
---------
0 1 0 1 = (5 in base 10)
The | is called bitwise OR. This works by:
Converting each number to binary
Doing boolean OR (||) on each digit in a matching position (0 is false, 1 is true)
Converting the result back to decimal
For example,
100 | 4
OR 001 | 1
-------+--
101 | 5
The properties on the Font constructor are designed so in binary, exactly one digit is a 1. By ORing these numbers, you get the digits turned on that represent the options that are ORed.

Categories