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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:31:02+00:00 2026-05-23T11:31:02+00:00

While I do understand endianness, I am slightly unclear on how the code works

  • 0

While I do understand endianness, I am slightly unclear on how the code works below. I guess this question is less about endianness and more about how the char * pointer and int work i.e. type conversion. Also, would it have made any difference if the variable word was not a short but just an int? Thanks!

#define BIG_ENDIAN 0
#define LITTLE_ENDIAN 1

int byteOrder() {
    short int word = 0x0001;
    char * byte = (char *) &word;
    return (byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}
  • 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-23T11:31:03+00:00Added an answer on May 23, 2026 at 11:31 am

    A short int is made up of two bytes, in this case 0x00 and 0x01. On a little endian system, the small byte comes first, so in memory it appears as 0x01 followed by 0x00. Big endian systems are, naturally, reversed. This is what the pointers look like for short integers on a little endian system:

    ----------------------- ----------------------- 
    |   0x01   |   0x00   | |          |          | 
    ----------------------- ----------------------- 
       &word                  &word+1
    

    Char pointers, on the other hand, are always incremented sequentially. Thus, by taking the address of the first byte of the integer and casting it to a char * pointer, you may increment through each byte of the integer in memory-order. Here’s the corresponding diagram:

    ------------ ------------ ------------ ------------ 
    |   0x01   | |   0x00   | |          | |          | 
    ------------ ------------ ------------ ------------ 
       &byte       &byte+1      &byte+2      &byte+3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I understand this question is fairly vague since I'm not giving you all
Jeff Atwood wrote about this here , and while I understand the theoretical performance
This may be more of a question about how Python itself imports methods, which
I am pondering about partial specialization . While I understand the idea, I haven't
While I understand the $this variable is not available when a method is called
I'm currently learning about pointers in my C++ Algorithms class and while I understand
Ok, so I've been writing PHP code for a little while and understand it
While I understand what simulation and emulation mean in general, I almost always get
I new to Scrum and while I understand the team concept behind the Sprints,
How do I find a stored procedure containing a certain text? While I understand

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.