Skip to content

Instantly share code, notes, and snippets.

@YuheiNakasaka
Created May 28, 2012 20:06
Show Gist options
  • Save YuheiNakasaka/2820977 to your computer and use it in GitHub Desktop.
Save YuheiNakasaka/2820977 to your computer and use it in GitHub Desktop.
class Parity
#parity.rb
class Parity
def initialize(num)
@bnum = num.to_s(2)
@counta = 0
0.upto(@bnum.length) do |i|
if @bnum[i] == "1"
@counta += 1
end
end
end
def odd_parity
print "odd_parity->"
if @counta.odd?
puts("0" + @bnum)
else
puts("1" + @bnum)
end
end
def even_parity
print "even_parity->"
if @counta.even?
puts("0" + @bnum)
else
puts("1" + @bnum)
end
end
end
#example
#number = Parity.new(11)
#number.odd_parity #->01101
#number.even_parity #->11101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment