LoginSignup
1
3

More than 5 years have passed since last update.

論理演算をrubyで表現するとこうなった

Last updated at Posted at 2017-09-23

コード

test.rb
require 'pp'
require 'optparse'

class Test

  def and(*args)
    args.all?{|arg| arg == 1} ? 1 : 0
  end

  def or(*args)
    args.any?{|arg| arg == 1} ? 1 : 0
  end

  def xor(*args)
    args.inject(:+) % 2 == 1 ? 1 : 0
  end

  def nand(*args)
    args.all?{|arg| arg == 1} ? 0 : 1
  end

  def nor(*args)
    args.any?{|arg| arg == 1} ? 0 : 1
  end

  def not(*args)
    args.map{|arg| if arg == 0; 1 elsif arg == 1; 0 end }
  end

end

@test = Test.new
argv_options = ARGV.getopts('l:', 'logic:')
argv_args = ARGV.map{|i| i.to_i}
logic = argv_options['l'] || argv_options['logic']

pp @test.send(logic, *argv_args)

論理積(AND)

全てが1であれば1を返す

x y z コマンド 出力
0 0 0 $ ruby test.rb -l and 0 0 0 0
0 0 1 $ ruby test.rb -l and 0 1 1 0
0 1 0 $ ruby test.rb -l and 0 1 0 0
0 1 1 $ ruby test.rb -l and 0 1 1 0
1 0 0 $ ruby test.rb -l and 1 0 0 0
1 0 1 $ ruby test.rb -l and 1 0 1 0
1 1 0 $ ruby test.rb -l and 1 1 0 0
1 1 1 $ ruby test.rb -l and 1 1 1 1

論理和(OR)

どれか一つでも1であれば1を返す

x y z コマンド 出力
0 0 0 $ ruby test.rb -l or 0 0 0 0
0 0 1 $ ruby test.rb -l or 0 1 1 1
0 1 0 $ ruby test.rb -l or 0 1 0 1
0 1 1 $ ruby test.rb -l or 0 1 1 1
1 0 0 $ ruby test.rb -l or 1 0 0 1
1 0 1 $ ruby test.rb -l or 1 0 1 1
1 1 0 $ ruby test.rb -l or 1 1 0 1
1 1 1 $ ruby test.rb -l or 1 1 1 1

排他的論理和(XOR)

1が偶数個の場合は0を返し、奇数個の場合は1を返す(自信ない。。)

x y z コマンド 出力
0 0 0 $ ruby test.rb -l xor 0 0 0 0
0 0 1 $ ruby test.rb -l xor 0 1 1 1
0 1 0 $ ruby test.rb -l xor 0 1 0 1
0 1 1 $ ruby test.rb -l xor 0 1 1 0
1 0 0 $ ruby test.rb -l xor 1 0 0 1
1 0 1 $ ruby test.rb -l xor 1 0 1 0
1 1 0 $ ruby test.rb -l xor 1 1 0 0
1 1 1 $ ruby test.rb -l xor 1 1 1 1

否定論理積(NAND)

ANDの逆

x y z コマンド 出力
0 0 0 $ ruby test.rb -l nand 0 0 0 1
0 0 1 $ ruby test.rb -l nand 0 1 1 1
0 1 0 $ ruby test.rb -l nand 0 1 0 1
0 1 1 $ ruby test.rb -l nand 0 1 1 1
1 0 0 $ ruby test.rb -l nand 1 0 0 1
1 0 1 $ ruby test.rb -l nand 1 0 1 1
1 1 0 $ ruby test.rb -l nand 1 1 0 1
1 1 1 $ ruby test.rb -l nand 1 1 1 0

否定論理和(NOR)

ORの逆

x y z コマンド 出力
0 0 0 $ ruby test.rb -l nor 0 0 0 1
0 0 1 $ ruby test.rb -l nor 0 1 1 0
0 1 0 $ ruby test.rb -l nor 0 1 0 0
0 1 1 $ ruby test.rb -l nor 0 1 1 0
1 0 0 $ ruby test.rb -l nor 1 0 0 0
1 0 1 $ ruby test.rb -l nor 1 0 1 0
1 1 0 $ ruby test.rb -l nor 1 1 0 0
1 1 1 $ ruby test.rb -l nor 1 1 1 0

否定(NOT)

前述の演算たちとは違い、ビット毎に適用される規則らしい(よくわかっていない)

x y z コマンド 出力
0 0 0 $ ruby test.rb -l not 0 0 0 [1, 1, 1]
0 0 1 $ ruby test.rb -l not 0 1 1 [1, 1, 0]
0 1 0 $ ruby test.rb -l not 0 1 0 [1, 0, 1]
0 1 1 $ ruby test.rb -l not 0 1 1 [1, 0, 0]
1 0 0 $ ruby test.rb -l not 1 0 0 [0, 1, 1]
1 0 1 $ ruby test.rb -l not 1 0 1 [0, 1, 0]
1 1 0 $ ruby test.rb -l not 1 1 0 [0, 0, 1]
1 1 1 $ ruby test.rb -l not 1 1 1 [0, 0, 0]

終わりに

参考にさせていただいたサイト:2進数と遊ぶ - 論理演算(ビット演算)

作るのに結構時間掛かった。。

間違っている箇所やもっと綺麗な書き方、コードが冗長になったとしてもより適切な論理演算の表現がありましたらコメント貰えるとうれしいです!

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