Bit field for Ruby-FFI
gem install ffi-bitfield
Classes
- class BitStruct < FFI::Struct
- class ManagedBitStruct < FFI::ManagedStruct
Loading
require 'ffi/bit_struct'
require 'ffi/managed_bit_struct'
Define your struct
class Struct1 < FFI::BitStruct
layout \
:a, :uint8,
:b, :uint8
bit_fields :a,
:u, 2,
:v, 2,
:w, 1,
:x, 1,
:y, 1,
:z, 1
end
Reading
s = Struct1.new
s[:a] = 63
p s[:u] # 3
p s[:v] # 3
p s[:w] # 1
p s[:x] # 1
p s[:y] # 0
p s[:z] # 0
Writing
s = Struct1.new
s[:u] = 0
s[:v] = 0
s[:w] = 0
s[:x] = 0
s[:y] = 1
p s[:a] # 64
require 'ffi/bit_field'
The above is the same as below.
require 'ffi/bit_struct'
require 'ffi/managed_bit_struct'
git clone https://github.com/kojix2/ffi-bitfield
cd ffi-bitfield
bundle install
bundle exec rake test
Your feedback is important.
ffi-bitfield is a library under development, so even small improvements like typofix are welcome! Please feel free to send us your pull requests. Bug reports and pull requests are welcome on GitHub at https://github.com/kojix2/ffi-bitfield.
Do you need commit rights to my repository?
Do you want to get admin rights and take over the project?
If so, please feel free to contact me @kojix2.
The gem is available as open source under the terms of the MIT License.