Thread: Endianess of machine

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12

    Endianess of machine

    How can i find out if machine is big/little endian with C... i know big/little endian but dont know how to write program for it. I dont want code but just point me in right direction.

    felipe

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since endianess works on a byte-level, that means anything larger than one byte will be affected.
    So create an integer of >= 2 bytes, fill it with a known value, then check its bytes one by one. Depending on the endianess, they will be appear in different order.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    By "checking its bytes" as Elysia suggested, it should be accessed one byte at a time after casting an integer to a byte array and looking at those elements. Or using a union to access the data type as integer or bytes.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    We would 'and' the variable against a known value (something with the high or low bits set e.g. 0x000F) and evaluate the result.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12
    Quote Originally Posted by Elysia View Post
    Since endianess works on a byte-level, that means anything larger than one byte will be affected.
    So create an integer of >= 2 bytes, fill it with a known value, then check its bytes one by one. Depending on the endianess, they will be appear in different order.
    If I have like int n=100... then what function is there in the C lib to check its byte one by one.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    As nonoob explained you can check the bytes of an integer by casting it to a char*.
    Code:
        short data = 0x1020;
        char *p = (char *)&data;
    Now p[0] will be the first byte in the representation of data, and p[1] will be the second. If p[0] is 0x20 the machine is little endian, for example.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12
    Quote Originally Posted by dwks View Post
    As nonoob explained you can check the bytes of an integer by casting it to a char*.
    Code:
        short data = 0x1020;
        char *p = (char *)&data;
    Now p[0] will be the first byte in the representation of data, and p[1] will be the second. If p[0] is 0x20 the machine is little endian, for example.
    If n is short integer and n=42 then in binary it look like 0000000000101010 as short 2 bytes long on my machine... so in big endian first byte is 00000000 (0) and in litlle endian it is 00101010 (42) correcto?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by fsanchez View Post
    If n is short integer and n=42 then in binary it look like 0000000000101010 as short 2 bytes long on my machine... so in big endian first byte is 00000000 (0) and in litlle endian it is 00101010 (42) correcto?
    Yep!

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Miami
    Posts
    12
    gracias a todos and specially to dwks.

  10. #10
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Sorry, just read the part about not wanting any code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Turing machine
    By Dante Wingates in forum General Discussions
    Replies: 5
    Last Post: 07-26-2010, 01:47 AM
  2. Will my memory leak? --school project
    By steals10304 in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2010, 03:04 PM
  3. Using ip tables to forward port to virtual machine
    By sean in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-31-2009, 09:16 AM
  4. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  5. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM