Module: Ronin::Support::Binary::BitFlip::Integer

Defined in:
lib/ronin/support/binary/bit_flip.rb

Overview

Methods for bit-flipping Integers.

Class Method Summary collapse

Class Method Details

.bit_flips(int, bits) ⇒ Array<Integer>

Returns every bit flip in the integer.

Examples:

bit-flip all eight bits:

0x41.bit_flips(8)

bit-flip bits 8-16:

0xffff.bit_flips(8...16)

Parameters:

  • bits (Integer, Range(Integer))

    The number of bits to flip or a range of bit indexes to flip.

Returns:

Raises:

  • (ArgumentError)

    The given bits must be either a Range or an Integer.

See Also:



102
103
104
# File 'lib/ronin/support/binary/bit_flip.rb', line 102

def self.bit_flips(int,bits)
  each_bit_flip(int,bits).to_a
end

.each_bit_flip(int, bits) {|int| ... } ⇒ Enumerator

Enumerates over every bit flip in the integer.

Examples:

bit-flip all eight bits:

Binary::BitFlip::Byte.each_bit_flip(0x41,8) { |int| puts "%.8b" % int }

bit-flip bits 8-16:

Binary::BitFlip::Byte.each_bit_flip(0xffff,8...16) { |int| puts "%.16b" % int }

Parameters:

  • bits (Integer, Range(Integer))

    The number of bits to flip or a range of bit indexes to flip.

Yields:

  • (int)

    If a block is given, it will be passed each bit-flipped integer.

Yield Parameters:

  • int (Integer)

    The integer but with one of it's bits flipped.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.

Raises:

  • (ArgumentError)

    The given bits must be either a Range or an Integer.

See Also:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ronin/support/binary/bit_flip.rb', line 65

def self.each_bit_flip(int,bits,&block)
  return enum_for(__method__,int,bits) unless block_given?

  bits = case bits
         when ::Range   then bits
         when ::Integer then (0...bits)
         else
           raise(ArgumentError,"bits must be an Integer or a Range: #{bits.inspect}")
         end

  bits.each do |bit_index|
    mask = 1 << bit_index

    yield int ^ mask
  end
end