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

  • SEARCH
  • Home
  • 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 8206839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:48:37+00:00 2026-06-07T08:48:37+00:00

My question concerns bit manipulation when the endianess changes. In particular I have some

  • 0

My question concerns bit manipulation when the endianess changes. In particular I have some code that reads individual bits of a uint32_t value and performs bit manipulation on them. The purpose was UTF-8 encoding. It works perfectly for my little endian machine.

Revisiting the code recently it dawned on me that I was not considering endianess of the machine as far as the uint32_t value’s bit representation is concerned. So I have some questions regarding that respect.

Let’s assume an example code that just requires bits 7-10 of an uint32_t saved in a different byte.

uint32_t v;
v = 18341;
char c = (v &(uint32_t) 0x3C0)>>6;

For little endian the number 18341 is represented as 0x47A5 or in binary:

0100 0111 1010 0101

and the above code should give us 1110 stored in the char

Now the question is how would we achieve this in a Big Endian machine? The same number would be represented quite differently 0xA5470000 or in binary:

1010 0101 0100 0111 0000 0000 0000 0000

with the bits we seek being in totally different positions and not even consequent.

Instead of using 0x3C0 at the other side of & we would have to use something else
since the byte order is different. And especially since we need consequent bits of a byte we would require multiple boolean & operations like below right?

char c = ((v&(uint32_t)0xc0)>>6) | ((v&(uint32_t)0x300)>>6)

Summing up. Is my understanding correct that in the cases where we need to get sequential bits of an integer value represented in binary we would need to perform different manipulations for the two endianess cases?

Finally is there a better way to achieve the same thing than the one I showed above? Maybe I am missing something totally obvious.

  • 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-07T08:48:39+00:00Added an answer on June 7, 2026 at 8:48 am

    No. If you are using values (like 0x300) and language operators (<<, |, &) it does not matter because the value will be represented according to the machine. So in your case you do not need to worry about this problem. You should worry, for example, when you are copying bytes from file into the memory.

    If you are dealing with the memory representation directly, you can convert the representation before manipulation:

    #if defined (BENDIAN)
       val = makelittle(val);
    #endif
       manip_lendian(val);
    #if defined (BENDIAN)
       val = makebig(val);
    #endif
    

    Also see this answer

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

Sidebar

Related Questions

My question concerns markup that surrounds some of the default phone number labels in
Question context: let say that there is some really important row in config/locales/en.yml that
This question concerns running python files in terminal that are not stored in the
ATM, I'm searching through open source/close source android applications to identify particular concerns (code
My question is a bit specific, I have a ListView of items above a
We have an application that now has a 64 bit target, as well as
This question concerns converting a floating point number that is less than abs(1) and
I have an ETL process that is, for this question's purpose, done entirely in
This question concerns a Tomcat 7 web application, which is connected to a MySQL
My question concerns Google Web Toolkit (GWT). I'm about to begin development for a

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.