Delphi Convert Set Of Bits as TBits to Integer or unsigned int

Ihave value from nor or xor gate with represented as TBits and i want to convert it to generic variable like integer or unsigned integer my current working ide Tokyo 10.2

var
  ABits: TBits;
  AComulative: UInt32;

const
  PosBitFromSensor1 = 0;
  PosBitFromSensor2 = 1;

begin
  ABits := TBits.Create;
  try
    ABits.Size := 32;
    {GetValFromSensor return Boolean type}
    ABits.Bits[PostBitFromSensor1] := GetValFromSensor(PosBitFromSensor1); 
    ABits.Bits[PostBitFromSensor2] := GetValFromSensor(PosBitFromSensor2);
    AComulative := SomeBitsConvertToInteger(ABits); {some function like this}
  finally
    ABits.Free;
  end;
end;

or any simple solution.

It won't be very fast but you can do regular bit manipulation, set each bit that corresponds to a "true" in the boolean array . For example:

function SomeBitsConvertToInteger(ABits: TBits): UInt32;
var
  i: Integer;
begin
  if ABits.Size <> SizeOf(Result) * 8 then
    raise EBitsError.Create('Invalid bits size');
  Result := 0;
  for i := 0 to Pred(SizeOf(Result) * 8) do
    Result := Result or UInt32((Ord(ABits[i]) shl i));
end;

Delphi Convert Set Of Bits as TBits to Integer or unsigned int, Delphi Convert Set Of Bits as TBits to Integer or unsigned int - delphi. For example: function SomeBitsConvertToInteger(ABits: TBits): UInt32; var i: Integer;​  The TBits type holds a flexible collection of bits (Boolean values). The colection size can be altered at any time (using the size property). Bits are accessed using the Bits property, like this :

this solution provided by @Victoria and @LURD it maybe usefull for the other that have same solving problem. sorry about my English.

type
  TBitsHelper = class helper for TBits
  public
    function ToUInt32: UInt32;
  end;

{ TBitsHelper }

function TBitsHelper.ToUInt32: UInt32;
type
  PUInt32 = ^UInt32;
begin
  if Size > SizeOf(Result) * 8 then
    raise EOverflow.Create('Size overflow!');
  with Self do
    Result := PUInt32(FBits)^;
end;

Internal Data Formats (Delphi) - RAD Studio, Delphi Convert Set Of Bits as TBits to Integer or unsigned int. Ihave value from nor or xor gate with represented as TBits and i want to convert it to generic  The Integer type is an Integer whose size is not guaranteed. It is the basic integer type in Delphi, and currently has the same capacity as the LongInt type - 1 bit sign, and 31 bits value. To hold very large integers, use the Int64 type.

maybe something like this :

type

  {$IF CompilerVersion > 32} // tokyo
    {$MESSAGE Fatal 'Check if TBits still has the exact same fields and adjust the IFDEF'}
  {$ENDIF}
  TPrivateAccessBits = class
  public
    FSize: Integer;
    FBits: Pointer;
  end;

Move(@TPrivateAccessBits(ABits).FBits, AComulative, sizeOf(AComulative));

Byte command, The following topics describe the internal formats of Delphi data types. All bits of unsigned integer types occupy by the magnitude of the value and have no other meaning. Shortint and Int8 are 1-byte (8-bit) signed integer numbers. A set is a bit array where each bit indicates whether an element is in the set or not. The LongInt type is a 32 bit signed Integer. This size is fixed, and will not change in future releases of Delphi. It is currently the same size as the Integer type. To hold very large signed integers, use the Int64 type. Download this web site as a Windows program.

[PDF] Delphi Basics list of RTL commands, The basic Integer type. LongInt, An Integer whose size is guaranteed to be 32 bits Set the minimum and maximum values of this data type min := Low(Byte); Thanks TLama, I’m aware of the TBits class (since Delphi 7), this entry was written back in 2009 and was provided to work with older versions of Delphi.

Delphi Basics : RTL By category : Types, Gives the absolute value of a number (-ve sign is removed). Directive. Abstract Converts TDateTime date and time values to a string. Procedure Generate a TDateTime value set to the very end of a month Int. The integer part of a floating point number as a float. Type. System. Int64. A 64 bit A 32 bit unsigned integer. The basic unsigned integer type: Int64: A 64 bit sized integer - the largest in Delphi: Integer: The basic Integer type: LongInt: An Integer whose size is guaranteed to be 32 bits: LongWord: A 32 bit unsigned integer: SmallInt: An Integer type supporting values from -32768 to 32767: Word: An integer type supporting values 0 to 65535

Conversion bits, System · Integer, The basic Integer type. System · LongInt, An Integer whose size is guaranteed to be 32 bits. System · LongWord, A 32 bit unsigned integer. The Int64 type is a 64 bit signed Integer. This size is fixed, and will not change in future releases of Delphi. Functions such as IntToStr support Int64 (via overloading). Integer constants are cast to Int64 type when they exceed Integer size. UInt64, the unsigned version of Int64 appears in Delphi after Delphi 7, it appears. #N#Related commands.

Comments
  • With a class helper you might do something like.
  • @LURD, that won't work in Tokyo. But you can write with Self do for this crack.
  • @Victoria, thanks. Fixed.
  • @LURD, noticed :)
  • There's no need for typecast. You'd just write AComulative := ABits.ToUInt32; as the result is already UInt32 ;-)
  • This is very unsafe and may break in any future version of the run-time library.
  • The class helper, being already failed once, is probably likelier to fail in the future.
  • @Sertac: But the ''mode'' of failure would likely be much nicer (won't compile vs "very strange behaviour somewhere in the code")!
  • I add the $MESSAGE WARN to make this code very safe !
  • No, staying safe or not is a matter of personal taste. Definition of safety is not. You cannot bypass an error, you can bypass a warning.