Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 799885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:12:13+00:00 2026-05-14T23:12:13+00:00

I am using a TStream to read binary data (thanks to this post: How

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:12:14+00:00Added an answer on May 14, 2026 at 11:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is "no" clean way to do this. We tried… May 14, 2026 at 11:12 pm
  • Editorial Team
    Editorial Team added an answer One advantage would be that a user may already have… May 14, 2026 at 11:12 pm
  • Editorial Team
    Editorial Team added an answer Yes it is safe to do so. May 14, 2026 at 11:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.