@@ -644,6 +644,40 @@ def _getue(self) -> int:
644644 # Convert back to unsigned value
645645 return code_bits + (1 << i ) - 1
646646 raise bitstring .InterpretError ("Cannot find any 1 bits in exponential-Golomb code." )
647+
648+ def _setsie (self , value : int , length : Optional [int ]= None ) -> None :
649+ """Reset the bitstring to have given signed interleaved exponential-Golomb code interpretation."""
650+ if length is not None :
651+ raise ValueError ("Length cannot be specified for signed interleaved exponential-Golomb codes." )
652+ # Convert to unsigned by mapping negative values to positive ones
653+ unsigned = abs (value ) << 1
654+ if value < 0 :
655+ unsigned -= 1
656+ # Get the number of bits needed for the unsigned value
657+ num_bits = unsigned .bit_length ()
658+ # Add leading zeros and the code
659+ self ._bitstore = BitStore (num_bits * 2 )
660+ self ._bitstore .setall (0 )
661+ # Set the code bits
662+ for i in range (num_bits - 1 ):
663+ self ._bitstore .setindex (num_bits - 1 + i , (unsigned >> (num_bits - 2 - i )) & 1 )
664+ # Set the terminating 1 bit
665+ self ._bitstore .setindex (num_bits - 1 , 1 )
666+
667+ def _getsie (self ) -> int :
668+ """Return data as a signed interleaved exponential-Golomb code."""
669+ # Find the first 1 bit
670+ for i in range (len (self )):
671+ if self ._bitstore .getindex (i ):
672+ # Get the code bits
673+ code_bits = 0
674+ for j in range (i + 1 , min (2 * i + 1 , len (self ))):
675+ code_bits = (code_bits << 1 ) | self ._bitstore .getindex (j )
676+ # Convert back to unsigned value
677+ unsigned = code_bits + (1 << i ) - 1
678+ # Map back to signed value
679+ return - (unsigned >> 1 ) - 1 if unsigned & 1 else unsigned >> 1
680+ raise bitstring .InterpretError ("Cannot find any 1 bits in exponential-Golomb code." )
647681 pass
648682
649683 def _getuint (self ) -> int :
0 commit comments