How to convert Big Endian and how to flip the highest bit?

I am using TStream to read binary data (thanks to this post: How to use TFileStream to read 2D matrices into a dynamic array? ).

My next problem is that the data is Big Endian. From my reading, the Swap () method seems to be deprecated. How do I change the types below?

 16-bit two complement binary integer
 32-bit two complement binary integer
 64-bit two complement binary integer
 IEEE single precision floating-point - Are IEEE affected by Big Endian?

And finally, since the data is unsigned, the creators of this data set saved unsigned values ​​as integers (excluding IEEE). They instruct that you only need to add an offset (2 ^ 15, 2 ^ 31 and 2 ^ 63) to restore unsigned data. But, they notice that switching the most significant bit is the fastest way to do this. How to effectively flip the most significant bit of a 16, 32, or 64-bit integer?

So, if the data on the disk (16 bits) is equal to "85 FB" - the desired result after reading the data and exchanging and reversing the bits will be 1531.

Is there a way to exchange and switch bits using generics so that it fits into the general answer at the link above?

, , , , ESO . FITS , - !

+4
2

:

function Flip16(const Value: Word): Word; inline;
begin
  Result:= Value xor $8000;
end;

function Flip32(const Value: LongWord): LongWord; inline;
begin
  Result:= Value xor $80000000;
end;

function Flip64(const Value: UInt64): UInt64; inline;
begin
  Result:= Value xor $8000000000000000;
end;

function SwapBytes(Value: LongWord): Single;
type
  Bytes = packed array[0..3] of Byte;

begin
  Bytes(Result)[0]:= Bytes(Value)[3];
  Bytes(Result)[1]:= Bytes(Value)[2];
  Bytes(Result)[2]:= Bytes(Value)[1];
  Bytes(Result)[3]:= Bytes(Value)[0];
end;

: , :)

function SwapBytes(Value: LongWord): Single; register; 
asm
  BSWAP  EAX
end;

:

​​SO - .

1) IEEE Big Endian?

, , , - .

2) - , , , , , , .. xor $80;

3) ;

16- Swap - , delphi help , ; XCHG asm, ;

32- Marco;

64- Marco :

function Swap64(Value: UInt64): UInt64;
begin
  Result:= Swap32(LongWord(Value));
  Result:= (Result shl 32) or Swap32(LongWord(Value shr 32));
end;

4) ?

, .

+7

, , . XOR , 0, 1 , . , $80 , $8000 , , $80000000 .

0

All Articles