Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

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

My next problem is that the data is Big Endian. From my reading, the Swap() method is seemingly deprecated. How would I swap the types below?

 16-bit two's complement binary integer
 32-bit two's complement binary integer
 64-bit two's 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 dataset have stored the unsigned values as signed integers (excluding the IEEE). They instruct that one need only add an offset (2^15, 2^31, and 2^63) to recover the unsigned data. But, they note that flipping the most significant bit is the fastest way to do that. How does one efficiently flip the most significant bit of a 16, 32, or 64-bit integer?

So, if the data on disk (16-bit) is "85 FB" - the desired result after reading the data and swapping and bit flipping would be 1531.

Is there a way to accomplish the swapping and bit flipping with generics so it fits into the generic answer at the link above?

Yes, kids, THIS is how scientific astronomical data is stored by NASA, ESO, and all professional astronomers. This FITS standard is considered by some to be one of the most successful standards ever created in its proliferation and flexibility!

like image 411
RobertFrank Avatar asked May 21 '10 13:05

RobertFrank


People also ask

How do you switch Endians?

To do this, we shift the rightmost 8 bits by 24 to the left so that it becomes the leftmost 8 bits. We left shift the right middle byte by 16 (to store it as the left middle byte) We left shift the left middle byte by 8 (to store it as the right muddle byte) We finally left shift the leftmost byte by 24 to the left.

Does endianness affect bit shifting?

So when it comes to bit-shifting, endianness doesn't matter [as long as you are shifting the number in one unit]. It's only when reading/writing memory that endianness makes a difference - the bytes from a big number either go out "big end" or "little end" first.

Where is the most significant bit in big-endian?

Big Endian Byte Order: The most significant byte (the "big end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory.

Is big-endian left to right?

Big-endian systems store data in memory the same way we humans think about data (left-to-right), which makes low-level debugging easier.


1 Answers

Not tested:

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;

Updated: Well if you about to optimize :)

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

Updated once more:

A common practice in SO is one question per one post - it generally results in better answers.

1) Are IEEE affected by Big Endian?

Yes, floating-point values can be Big Endians, but it does not mean that your floating-point values are Big Endians - check your documentation.

2) flipping a most significant bit - the answer is already given, but since you probably have Big Endians you may need just to flip a most significant bit in a least significant byte, i.e. just xor $80;

3) converting big endian to little endian;

for 16-bit values use Swap function - it is hard to understand why delphi help says that the function is for backward compatibility only; it is also possible to use XCHG asm instruction as Remko noticed;

for 32-bit values your can use my code or code in Marco's comment;

for 64-bit values you can use a modification of Marco's code like this:

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

4) Is it possible to use generics here?

I don't think it is a good idea.

like image 54
kludg Avatar answered Oct 03 '22 01:10

kludg