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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:16:12+00:00 2026-06-18T05:16:12+00:00

Possible Duplicate: Little vs Big Endianess: How to interpret the test Is there an

  • 0

Possible Duplicate:
Little vs Big Endianess: How to interpret the test

Is there an easy method to test code with gcc or any online compiler like ideone for big endian? I don’t want to use qemu or virtual machines

EDIT

Can someone explain the behavior of this piece of code on a system using big endian?

#include <stdio.h>
#include <string.h>
#include <stdint.h>

int main (void)
{
    int32_t i;
    unsigned char u[4] = {'a', 'b', 'c', 'd'};

    memcpy(&i, u, sizeof(u));
    printf("%d\n", i);
    memcpy(u, &i, sizeof(i));
    for (i = 0; i < 4; i++) {
        printf("%c", u[i]);
    }
    printf("\n");
    return 0;
}
  • 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-18T05:16:14+00:00Added an answer on June 18, 2026 at 5:16 am

    As a program?

    #include <stdio.h>
    #include <stdint.h>
    
    int main(int argc, char** argv) {
        union {
           uint32_t word;
           uint8_t bytes[4];
        } test_struct;
        test_struct.word = 0x1;
        if (test_struct.bytes[0] != 0)
            printf("little-endian\n");
        else
            printf("big-endian\n");
        return 0;
    }
    

    On a little-endian architecture, the least significant byte is stored first. On a big-endian architecture, the most-significant byte is stored first. So by overlaying a uint32_t with a uint8_t[4], I can check to see which byte comes first. See: http://en.wikipedia.org/wiki/Big_endian

    GCC in particular defines the __BYTE_ORDER__ macro as an extension. You can test against __ORDER_BIG_ENDIAN__, __ORDER_LITTLE_ENDIAN__, and __ORDER_PDP_ENDIAN__ (which I didn’t know existed!) — see http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html

    See also http://en.wikipedia.org/wiki/Big_endian


    As for running code in an endianness that doesn’t match your machine’s native endianness, then you’re going to have to compile and run it on an architecture that has that different endianness. So you are going to need to cross-compile, and run on an emulator or virtual machine.


    edit: ah, I didn’t see the first printf().

    The first printf will print “1633837924”, since a big-endian machine will interpret the 'a' character as the most significant byte in the int.

    The second printf will just print “abcd”, since the value of u has been copied byte-by-byte back and forth from i.

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

Sidebar

Related Questions

Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: hiding strings in Obfuscated code I'm trying to hide a little some
Possible Duplicate: convert big endian to little endian in C [without using provided func]
Possible Duplicate: Which keycode for escape key with jQuery This little piece of code
Possible Duplicate: alloc, init, and new in Objective-C I am a little confused about
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: When does Endianness become a factor? reading this tuto on endianess, i
Possible Duplicate: Ruby.Metaprogramming. class_eval I have this little project, the goal is to create
Possible Duplicate: MATLAB: How To Efficiently Remove NaN Elements from Matrix I got code
Possible Duplicate: javascript - shuffle HTML list element order After a little bit of

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.