Class: Protobuf::Field::SignedIntegerField

Inherits:
VarintField show all
Defined in:
lib/protobuf/field/signed_integer_field.rb

Direct Known Subclasses

Sint32Field, Sint64Field

Constant Summary

Constants inherited from VarintField

VarintField::INT32_MAX, VarintField::INT32_MIN, VarintField::INT64_MAX, VarintField::INT64_MIN, VarintField::UINT32_MAX, VarintField::UINT64_MAX

Constants inherited from BaseField

BaseField::PACKED_TYPES

Instance Attribute Summary

Attributes inherited from BaseField

#default, #default_value, #deprecated, #extension, #getter_method_name, #message_class, #name, #optional, #packed, #repeated, #required, #rule, #setter_method_name, #tag, #type_class

Instance Method Summary collapse

Methods inherited from VarintField

#acceptable?, default, encode, #wire_type

Methods inherited from BaseField

#acceptable?, default, #deprecated?, #enum?, #extension?, #initialize, #message?, #optional?, #packed?, #repeated?, #repeated_message?, #required?, #set, #to_s, #type, #warn_if_deprecated

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Instance Method Details

#decode(value) ⇒ Object

Public Instance Methods



11
12
13
14
15
16
17
# File 'lib/protobuf/field/signed_integer_field.rb', line 11

def decode(value)
  if (value & 1).zero?
    value >> 1   # positive value
  else
    ~value >> 1  # negative value
  end
end

#encode(value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/protobuf/field/signed_integer_field.rb', line 19

def encode(value)
  if value >= 0
    VarintField.encode(value << 1)
  else
    VarintField.encode(~(value << 1))
  end
end