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 8626567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:03:50+00:00 2026-06-12T08:03:50+00:00

Currently I have this function to swap the bytes of a data in order

  • 0

Currently I have this function to swap the bytes of a data in order to change endianness.

template<typename Type, unsigned int Half = sizeof(Type)/2, unsigned int End = sizeof(Type)-1> 
inline void swapBytes(Type& x)
{
    char* c = reinterpret_cast<char*>(&x);
    char tmp;
    for (unsigned int i = 0; i < Half; ++i) {
        tmp = c[i];
        c[i] = c[End-i];
        c[End-i] = tmp;
    }
}

This function will be called by some algorithms of mine several million times. Consequently, every single instruction that can be avoided would be a good thing.

My question is : how can this function be optimized ?

  • 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-06-12T08:03:51+00:00Added an answer on June 12, 2026 at 8:03 am

    First of all you need to check if your hardware platform have byte swap instructions or not. Some platforms have these instructions, some of them not. After that you need to look for library function that uses them. Check the docs or stop in the debugger and look at the disassembly. It is a good chance that you will find one. It is unlikely that anything else will work better than this.

    Ultimately write your own function in assembler that uses these instructions.

    For a 2-byte type a straight table conversion will work. This is 128 kb that is not that much for our days computers. For 32 bit types this is close to overkill but in some (rare) cases may still work on a big 64-bit box.

    You can also use combination of asm instructions, table conversion and optimized loop.

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

Sidebar

Related Questions

I currently have this code: PACKETS = {}; function AddPacket(data) local id = data.ID;
I currently have this javascript: $(document).on('click', '.add_option', function(e){ e.preventDefault(); $.get(/options/render_option, function(data){ $('.add_option_control_group').before(data.option) $('#pollview').append(data.view) $(document).on('keyup',
I currently have this: function submit() { document.getElementById(lostpasswordform).click(); // Simulates button click document.lostpasswordform.submit(); //
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
Currently i have this: $(.splitCol).click(function () { $.cookie('whichColumn', 'split'); $(.threeCol .active).removeClass(active); $(.leftCol .active).removeClass(active); $(.splitCol
I'm currently debugging an Ms SQL Function (SQL 2008). In this function, I have
The code i current have is this. function update (){ latest_id = $('#image:first').data('position'); /*
I have this code public function TalentBox(x:int, y:int, arg_color:int = 0xFFFFFF):void { this.graphics.beginFill(arg_color); this.graphics.lineStyle(1.0,
I have this: function startLoad(object_id) { $j('div#'+object_id).addClass('currently-loading'); var con = $j('div#overlay'); var img =
I currently have a function that looks like this: public void AnimateLayoutTransform(object ControlToAnimate) {

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.