Like Tree6Likes

little endian query

This is a discussion on little endian query within the C Programming forums, part of the General Programming Boards category; Please don't use the words "left" and "right" when talking about endianness. If left and right mattered, I could alter ...

  1. #16
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Please don't use the words "left" and "right" when talking about endianness. If left and right mattered, I could alter the endianness of my machine by turning it upside down. What matters is whether the least significant component of the value is stored at the lowest memory address (little endian) or the highest memory address (big endian). Since bits are not addressable, the question of bit-wise endianness has no meaning.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by brewbuck View Post
    Please don't use the words "left" and "right" when talking about endianness. If left and right mattered, I could alter the endianness of my machine by turning it upside down.
    :eyebrow:
    Quote Originally Posted by brewbuck View Post
    What matters is whether the least significant component of the value is stored at the lowest memory address (little endian) or the highest memory address (big endian).
    So if I tip my computer over on its side, did I just change what was low and what was high?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    So if I tip my computer over on its side, did I just change what was low and what was high?
    Quzah.
    Naaa... but you probably just dumped all the dust inside out onto your carpet...
    quzah, laserlight and sed_y like this.

  4. #19
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by quzah View Post
    :eyebrow:So if I tip my computer over on its side, did I just change what was low and what was high?


    Quzah.
    Damn. You got me.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by anduril462 View Post
    Bit-endianness is usually only discussed in things like transmission protocols (serial, USB, I2C).
    Yep! especially when interfacing a big-endian micro with a little-endian peripheral like the RS232 serial interface.
    Quote Originally Posted by anduril462 View Post
    CPUs rarely allow bit-level addressing, so it doesn't make sense to care about or check for bit-endianness. AFAIK, it's always assumed to be big bit-endian.
    There are plenty of microcontrollers 'n microprocessors that are little endian and in 'em a byte will be stored with the LSB at the lowest memory slot.
    Quote Originally Posted by anduril462 View Post
    I can't think of any way in C to determine bit-endianness of the architecture.
    Yep! for that one would have to drop down to the assembly level.
    Quote Originally Posted by anduril462 View Post
    Byte-endianness, which is regularly an issue in C programming, makes no sense whatsoever for a one-byte variable. There is only one byte, it's the first and last, and is both the least- and most-significant byte.
    For starters, the OP wanted to know how data is laid out in memory, not how it's interpreted or displayed.
    That being the case a byte in memory will be stored as per the endianness of the microarchitecture.
    Though its interpretation will always from the MSB to LSB, regardless of the micros endianness.
    Last edited by itCbitC; 09-02-2011 at 11:03 AM.

  6. #21
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by anduril462 View Post
    I can't think of any way in C to determine bit-endianness of the architecture.
    Would this work, or would the struct members switch places depending on endianness?
    Code:
    #include <stdio.h>
    
    union endiantest
    {
    	struct
    	{
    		unsigned char low : 1;
    		unsigned char pad : 6;
    		unsigned char high: 1;
    	};
    	unsigned char val;
    };
    
    int main()
    {
    	union endiantest test = {0};
    	if(sizeof(test) != 1)
    	{
    		printf("Sorry, your compiler doesn't support byte-sized bitfields\n");
    		return 1;
    	}
    	test.val = 1;
    	if(test.low)
    		printf("Little endian\n");
    	else if(test.high)
    		printf("Big endian\n");
    	else
    		printf("Weird endian\n");
    	return 0;
    }
    I'm a bit unsure of which is which, so I might have gotten big and little endian mixed up

  7. #22
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Unless individual bits can be ADDRESSED (not just accessed), the question has no meaning. Sure, I can access the MSB of a byte by AND'ing with 0x80, this still tells me nothing about how the bits are arranged in physical memory, nor is that even relevant. If the architecture allowed actual indexing of bits, such that ram[i] returned the i'th bit in RAM, then the architecture would have a bit-endianness. Otherwise, it does not. It's not that you can't measure it, it's that it's meaningless. Endianness is the relationship between significance and addressing. If there is no addressing there is no relationship.
    anduril462 and iMalc like this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by itCbitC View Post
    Yep! especially when interfacing a big-endian micro with a little-endian peripheral like the RS232 serial interface....
    Yeah, we're in agreement on one or two things. I don't think I was as clear as I could have been. I was making two points:
    1. Byte-endianness of an architecture is irrelevant for single-byte variables like char.
    2. Bit-endianness is impossible to determine in C, so discussion of it is frivolous.

  9. #24
    Registered User
    Join Date
    May 2011
    Posts
    44
    Quote Originally Posted by anduril462 View Post
    2. Bit-endianness is impossible to determine in C, so discussion of it is frivolous.
    can't i use something like below
    Code:
    #include <stdio.h>
    
    int main()
    {
        unsigned char ch[3] = "AB";  //0x410x42
        unsigned char *pch= ch;
        printf (" *pch or ch[0]= %x and *(pch+1) or ch[1]=%x\n ", *pch,*(pch+1));
        c= ch[0];
        printf("ch[0] bitwise is : \n");
        printf("ch[0](i)  bitval\n");
       for (i=0 ; i<sizeof(char)*8; i++)
       {  
    	   printf("%d       %x \n ",i,(c >> i)&0x01);
       }
    }
    see the result below
    Name:  figure_5.png
Views: 58
Size:  6.7 KB
    now,0x41 or 65 would be 01000001 , but the output above is 10000010.
    now, 1 is the LSB, and if it was stored at higher memory address, its value
    acessed using a char pointer would not be 65d but 130d. so, this shows that
    bits are little endian.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are getting 10000010 because you are printing your byte in reverse. Put another way: you can't say that 0x01 has the "last" bit on, because if the byte is stored the other way then you get the first bit on instead. The operations you are doing will give you exactly the same answer regardless of whether the bits are stored low-bit-first, low-bit-last, or low-bit-in-the-middle-spiraling-outward.

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's not bit-endianness. Consider this:
    Code:
    #include<stdio.h>
    int main( void )
    {
        union u
        {
            struct byte
            {
                unsigned int b0:1;
                unsigned int b1:1;
                unsigned int b2:1;
                unsigned int b3:1;
                unsigned int b4:1;
                unsigned int b5:1;
                unsigned int b6:1;
                unsigned int b7:1;
            } bm;
            unsigned char uc;
        } b;
        b.uc = 0xF0;
        
        printf( "%d%d%d%d %d%d%d%d\n",
            b.bm.b0, b.bm.b1, b.bm.b2, b.bm.b3,
            b.bm.b4, b.bm.b5, b.bm.b6, b.bm.b7 );
        
        return 0;
    }
    That still doesn't tell us anything, because foo & 1 is always going to result in 1 if the first bit is set, regardless on which end of the byte the bit is.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by sed_y View Post
    can't i use something like below
    No. You've heard several times, from several people, that bit-endianness can not be determined in C. You cannot address bits, there is no way to check endianness. End of story.
    now,0x41 or 65 would be 01000001 , but the output above is 10000010.
    now, 1 is the LSB, and if it was stored at higher memory address, its value
    acessed using a char pointer would not be 65d but 130d. so, this shows that
    bits are little endian.
    And as others pointed out, printing something backwards doesn't prove it's stored backwards, it just proves you can print it backwards.

  13. #28
    Registered User
    Join Date
    May 2011
    Posts
    44
    ok,
    all right.
    thanks

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endian
    By kiros88 in forum C Programming
    Replies: 18
    Last Post: 06-19-2010, 04:08 PM
  2. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 06:47 PM
  3. Big Endian & Little Endian
    By swaugh in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 11:25 PM
  4. Big Endian Little Endian Complex- Converting Characters
    By bd02eagle in forum C Programming
    Replies: 3
    Last Post: 07-11-2006, 01:01 AM
  5. Big endian/Little endian conversion
    By bonkey in forum Windows Programming
    Replies: 5
    Last Post: 09-25-2002, 02:29 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21