2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptのビットシフト(ビット演算)

Last updated at Posted at 2013-04-18

ビット演算はある数値の半分、倍のイディオムとして利用される

var num = 120
console.log(num >> 0); //120
console.log(num >> 1); //60
console.log(num >> 2); //30
console.log(num << 1); //240
console.log(num << 2); //480
console.log('abc' << 2); //文字列の場合は0が返るみたい

また、小数点を切り捨てるイディオムとしても利用される(たぶん)
(小数点以下は切り捨てられ、四捨五入はされない)

var num = 12.123
console.log(num >> 0); //12
console.log(num >> 1); //6
console.log(num >> 2); //3
console.log(num << 1); //24
console.log(num << 2); //48

※右辺に指定できるの数は0〜31まで

2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?