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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:09:39+00:00 2026-05-27T04:09:39+00:00

In a microcontroller project written in C, we defined the following macros to access

  • 0

In a microcontroller project written in C, we defined the following macros to access different bytes of a multi byte variable (4 byte long):

#define BYTE_0(var)         (*((unsigned char*) &var))
#define BYTE_1(var)         (*(((unsigned char*) &var) + 1))
#define BYTE_2(var)         (*(((unsigned char*) &var) + 2))

BYTE_0() accesses least significant byte, and so on. This is because we find that in case we need to access different bytes of a multi-byte variable separately (the micro in 8 bit), accessing the bytes using the code above produces fewer number of lines of code in assembly. As the code memory size is only 15K, few bytes are sometimes precious.

The micro we’re using is little-endian. I’m wondering if we port the code to another micro which is big-endian architecture, will the code above work? In other words, does C standard guarantee that (*((unsigned char*) &var)) will give the least significant byte of var?

  • 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-27T04:09:39+00:00Added an answer on May 27, 2026 at 4:09 am

    Your macro does not work, it assumes little endian architecture. The C standard guarantees nothing in the case of your code. Endian-independent code is typically written with bit-wise operators, because they behave the same way no matter where the ls byte is allocated.

    some_long & 0xFF is guaranteed by the C standard to give you the ls byte no matter endianess, while (uint8_t*)&some_long is endian-dependent.

    This link answers your question in detail: http://www.ibm.com/developerworks/aix/library/au-endianc/
    Do a macro similar to the one in listing 12 with bitwise shift and bitwise AND, and it will be portable.

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

Sidebar

Related Questions

I have the following code in my microcontroller project: if (newThrottle < currentThrottle) {
I've incorporated LIBPNG and ZLIB into my C software for a microcontroller project due
I have to make a hardware project using a microcontroller, memory, screens, etc. Is
I am working on a microcontroller project in C. The main.c file includes a
How do get ARM microcontroller port value into a 32 bit variable. I am
I am passing 64 byte data packets over USB to a microcontroller. In the
I am starting a NEW project into this complete new segment of microcontroller, so
Currently I am working on a project which has following setup. AVR micro-controller IDE
A basic question on MPC55xx microcontroller: I configured the reset boot vector (4 bytes
I'm working on a microcontroller DDS project in C and am having some trouble

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.