LoginSignup
0
0

More than 1 year has passed since last update.

シフトで負の値を与えるとどうなるのか?

Posted at

左シフトと右シフト(符号なし)に負値を与えてみます。

実行環境は macOS 12.1 (Intel版) のターミナル

ターミナル
$ node
Welcome to Node.js v16.13.1.
Type ".help" for more information.
> (1<<(-1)).toString(16)
'-80000000'
> (1<<(-2)).toString(16)
'40000000'
> (1<<(-3)).toString(16)
'20000000'
> (1<<(-31)).toString(16)
'2'
> (1<<(-32)).toString(16)
'1'
> (1<<(-33)).toString(16)
'-80000000'
> (0x1234<<(-4)).toString(16)
'40000000'
> (0x1234<<(-8)).toString(16)
'34000000'
> (0x1234<<(-16)).toString(16)
'12340000'
> (0x1234<<(-32)).toString(16)
'1234'
> (0x1234<<(-36)).toString(16)
'40000000'
> (1>>>(-1)).toString(16)
'0'
> (0x80000000>>>(-1)).toString(16)
'1'
> (0x80000000>>>(-2)).toString(16)
'2'
> (0x80000000>>>(-30)).toString(16)
'20000000'
> (0x80000000>>>(-31)).toString(16)
'40000000'
> (0x80000000>>>(-32)).toString(16)
'80000000'
> (0x80000000>>>(-33)).toString(16)
'1'
> (0xFEDC0000>>>(-1)).toString(16)
'1'
> (0xFEDC0000>>>(-4)).toString(16)
'f'
> (0xFEDC0000>>>(-8)).toString(16)
'fe'
> (0xFEDC0000>>>(-16)).toString(16)
'fedc'
> (0xFEDC0000>>>(-32)).toString(16)
'fedc0000'
> (0xFEDC0000>>>(-33)).toString(16)
'1'
> 1<<33
2

という結果が得られました。

仕様がどうなっているのか知らない(定義されてない気がする)けど、実際の挙動は

function left_shift(x, n) { return x << (n & 31); }
function right_shift(x, n) { return x >> (n & 31); }

となっているようです。プロセッサ依存だったりしないのかな?

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0