LoginSignup
3
1

More than 5 years have passed since last update.

CommonLispのash関数でビット演算

Posted at

ash関数概要

  • ash関数は、第一引数の整数を第二引数分だけビット移動する関数。
  • 例えば、第一引数に10を指定して、第二引数に1を指定すれば1ビット左に移動するので、20となる。
  • まあ1ビット移動すると2倍、2ビット移動すると4倍だ。確か。そうだよね??
  • 逆に、右に移動すると2分の1、4分の1となる。
;左シフト演算
(ash 5 1) ;10

(ash 10 1) ;20

(ash (+ 5 2) 2) ;28

;右シフト演算
(ash 5 -1) ;2

(ash 10 -2) ;5

(ash (+ 5 2) -2) ;1
整数 シフト 2進数 2進数シフト後 整数シフト後
5 2ビット左シフト 101 1010 10
10 2ビット左シフト 1010 10100 20
5+2 4ビット左シフト 111 11100 28
5 2ビット右シフト 101 10 2
10 2ビット右シフト 1010 101 5
5+2 4ビット 111 11 3

所感

  • 関数一つ調べても日本語の記事は全然でてこない。。
  • 調べたことは逐次Quitaに上げといたほうが個人的な資産になりそう(笑)
3
1
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
3
1