Why Java doesn't support <<< oprerato

2020-05-25 06:46发布

Why doesn't Java support the <<< (unsigned left shift) operator, but does support the >>> (unsigned right shift) operator?

标签: java oop
4条回答
别忘想泡老子
2楼-- · 2020-05-25 07:27

I think this is a design mistake. The << is not arithmetic shift. << is supposed multiply 2 if you do

0xF0FFFFFF << 4

it return a positive number! a negative number multiply 2 should never get a positive number. the << is doing <<< work.

查看更多
家丑人穷心不美
3楼-- · 2020-05-25 07:34

Why doesn't Java support the <<< (unsigned left shift) operator, but does support the >>> (unsigned right shift) operator?

Because a (hypothetical) unsigned left shift operator would do exactly the same thing as the existing left shift operator.

(Hint: multiplying a binary integer by 2 entails left shifting by 1 and making the rightmost bit zero whether the integer representation is signed or unsigned. Write some examples on a piece of paper and test it for yourself.)

查看更多
一夜七次
4楼-- · 2020-05-25 07:37

Why doesn't Java support the <<< (unsigned left shift) operator, but does support the >>> (unsigned right shift) operator?

That is because when you left shift the bits , the leftmost bit (AKA SIGNED Bit) is lost anyways.

Since unsigned left shift operator would do exactly the same thing as the existing left shift operator, we don't have it.

查看更多
Animai°情兽
5楼-- · 2020-05-25 07:45

Java adds the operator ">>>" to perform logical right shifts, but because the logical and arithmetic left-shift operations are identical, there is no "<<<" operator in Java.

from Shifts in Java...

查看更多
登录 后发表回答