Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Pointing out the correct statements (Q.No. 3)
3.
Which two statements are equivalent?
- 16*4
- 16>>2
- 16/2^2
- 16>>>2
Answer: Option
Explanation:
(2) is correct. 16 >> 2 = 4
(4) is correct. 16 >>> 2 = 4
(1) is wrong. 16 * 4 = 64
(3) is wrong. 16/2 ^ 2 = 10
Discussion:
27 comments Page 1 of 3.
Khan said:
7 years ago
16>>2 signed right shift.
16>>>2 unsigned right shift. The most significant bit in >> is sign bit, so that if the sign bit is 0, the number is positive and if the sign bit is 1 the number is negative.
Here, since the number is already positive when we shift by >>, we get 100 and when we use >>> we get 0100, the 0 before 1 represents that its a positive number. Had this been -16, the answer would have been 100 for >> and 1100 for >>>.
16>>>2 unsigned right shift. The most significant bit in >> is sign bit, so that if the sign bit is 0, the number is positive and if the sign bit is 1 the number is negative.
Here, since the number is already positive when we shift by >>, we get 100 and when we use >>> we get 0100, the 0 before 1 represents that its a positive number. Had this been -16, the answer would have been 100 for >> and 1100 for >>>.
(1)
Allauddin Pirjade said:
8 years ago
In simple words >>> always shifts a zero into the leftmost position whereas >> shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.
Neethu said:
8 years ago
16/2^2.
16/2=8->1000.
1000^0010=1010~=10.
16/2=8->1000.
1000^0010=1010~=10.
Gowthami said:
8 years ago
Answer is 16.
Santhosh said:
9 years ago
2<<3 what is the answer, can you explain me?
Priyanka Srivastava said:
9 years ago
Sir, please give the explanation about how 16 >>> 2 is equals 16 >> 2.
I do not understand the question.
I do not understand the question.
Sumit said:
10 years ago
Hi, how this is possible?
Prajwal said:
10 years ago
This work in hexa-decimal format.
Ex: We have 16 >>> 2.
0000 0000 0001 0000 --> 16.
Then 16 shifted by 2.
0000 0000 0000 0100 --> 4.
Observe 1 is shifted by 2.
Ex: We have 16 >>> 2.
0000 0000 0001 0000 --> 16.
Then 16 shifted by 2.
0000 0000 0000 0100 --> 4.
Observe 1 is shifted by 2.
Abhi said:
10 years ago
The >>> operator shifts the bits of expression 1 right by the number of bits specified in expression 2. Zeroes are filled in from the left. Digits shifted off the right are discarded.
Eod said:
1 decade ago
@Venkat, '^' operation is not Math.Exp, as @Utpal said before it's bit wise OR operation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers