7
7

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.

ビット演算子を使った小数の操作

Last updated at Posted at 2014-04-07

ビット演算子("&","|:"<<"等) は、計算対象を32ビットの整数に変換してから演算を行う。

こいつを利用してこんなことができる。

小数点以下切り捨て

var a =1.1;
a|=0; // こいつ
console.log(a); // 1が出力される

小数判定

result = (a===(a|0)); // 整数:true, 小数:false

ただし、32ビットへの変換なので、値が0x7fffffffより大きいと正しい結果が得られないですゾ。

7
7
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?