LoginSignup
1
2

More than 5 years have passed since last update.

プラットフォームのビット環境を調べる方法

Last updated at Posted at 2017-06-06

strconvパッケージでIntSizeという名前で定数定義されている

その導出方法は、書籍プログラミング言語Goの演習問題6.5で言及されているテクニックを使用している

32 << (^uint(0) >> 63) 

uintはプラットフォームが32bit環境なら32bitの符号無し整数・64bit環境なら64bitの符号無し整数として振る舞う

^uint(0)は32bit環境ならば0を反転しているので、32bit立った状態を表している
(^uint(0) >> 63)は63bit文右シフトしているので、32bitならば0になり、63bitならば最下位のbitだけが立っている状態になる
したがって、32 << (^uint(0) >> 63)は32bit環境ならば32 << 0 (32)となり、64bit環境ならば32 << 1 (64) となる

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