Is the endianess can be for the nibble boundaries ? Because i have defined a class, and try to access the array with a pointer of class type.

typedef unsigned char uint8;

class A{
uint8 a:4;
uint8 b:4;

int _tmain(int argc, _TCHAR* argv[])
    uint8 temp = 0xfa;

    A * ptr_a;

    ptr_a = (A *) &temp;

    printf("%x", ptr_a->a );


To my surprise, a is 0x0a and not '0x0f' !. Any comments on this, will be very helpful, as i have a very big structure with access to nibble boundaries, will make it more complex for me to determine which bit goes where !!!

Well to my understanding ptr_a->a would be the first 4 bits (lower 4) so it should indeed be 0x0A as when we read hex remember we read right to left. so when you assign 4bits and 4bits your working right to left i think.

Dont quote me on that but thats what i think your code is doing.

If you want your program to be reliable, I suggest you look for a different way of solving your problem. You have a pointer to one type (uint8) and you casted it to a pointer to a different type (A). Compilers are under no obligation to make such a cast behave in any particular way, and the more aggressive the compiler is about optimizing, the less obliged its authors feel.

Even if you wind up getting your program to do what you want, you may have difficulty moving it to a different implementation.

So if you want to treat an integer as if it were several separate data items crammed togeher, I think you would be better off using the and, or, and shift operators to do so in a portable way.

@arkoenig, thankyou very much for pointing out the problem with typecasting ..

yes , typecasting needs to be avoided !. As i am trying to modify the existing code, which is somehow working !, i donot want to touch that part as of now. I need to add another functionality, after that works, i plan to do this as part of the code cleanup.