Skip to content

Instantly share code, notes, and snippets.

@papes1ns
Created March 15, 2017 23:39
Show Gist options
  • Save papes1ns/0900ea5122ce90b3ff743249efbba61f to your computer and use it in GitHub Desktop.
Save papes1ns/0900ea5122ce90b3ff743249efbba61f to your computer and use it in GitHub Desktop.
flag_defs = [{
:name => "member1",
:position => 0,
:value => true
},
{
:name => "member2",
:position => 1,
:value => true
},
{
:name => "member3",
:position => 2,
:value => true
}]
flags_state = 0
def value(flag, flags_state)
flags_state & (1 << flag[:position]) > 0
end
def update_flag_state(flag_defs, flags_state)
flag_defs.each do |f|
if f[:value]
puts " doing #{flags_state.to_s(2)} |= #{(1 << f[:position]).to_s(2)} #{(flags_state | (1 << f[:position])).to_s(2)}"
flags_state |= (1 << f[:position])
else
puts " doing #{flags_state.to_s(2)} &= #{(~(1 << f[:position])).to_s(2)} #{(flags_state & ~(1 << f[:position])).to_s(2)}"
flags_state &= ~(1 << f[:position])
end
#puts " flags_state = #{flags_state.to_s(2)}"
end
flags_state
end
flags_state = update_flag_state(flag_defs, flags_state)
puts "flags_state=#{flags_state}"
flag_defs.each {|f| puts "#{f[:name]}=#{value(f, flags_state)}"}
fl=flag_defs.shuffle.first
fl[:value] = !fl[:value]
flags_state = update_flag_state(flag_defs, flags_state)
puts "flags_state=#{flags_state}"
flag_defs.each {|f| puts "#{f[:name]}=#{value(f, flags_state)}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment