2

I want to get answer 4, but my code prints 2. I don't know why?

public class Test {
    public static void main(String[] args) {
        System.out.println(9 >> 2);
    }
}

Image for my output and code

mike
  • 31
  • 3
  • 4
    Why do you expect 4? – ernest_k Feb 17 '21 at 04:57
  • 1
    Does this answer your question? [What are bitwise shift (bit-shift) operators and how do they work?](https://stackoverflow.com/questions/141525/what-are-bitwise-shift-bit-shift-operators-and-how-do-they-work) – SRJ Feb 17 '21 at 05:02

2 Answers2

1

Yes it will give you 2

because

9 binary representation is 1001, and when you applied the right shift by 2 it will give you 0010

thats why

9 >> 2 = 2
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
-2

9=0x1001 does that make the bit shift clear?

Bing Wang
  • 1,548
  • 1
  • 8
  • 7