-1

Revised Question -

How to calculate the mask-bits irrespective of the size of integer type? I want to calculate the mask of first 4-bits, when I don't the size of integer.

I have two options to set the MSB 4-bits in the code -

if little_Endian -

then --

  int t  = 54342;

    int k = t<<4;
    int t = (k>>4)|0XF000

else big Endian -- then --

 int t  = 54342;
    int k = t>>4;
    int t = (k<<4)|0X000F

My question is is there any better way to do so. How can I make the code independent of the endianity? I can use union to determine the endianity. However, I want my code to simple. How can I do so?

dexterous
  • 6,422
  • 12
  • 51
  • 99

1 Answers1

2

Endianess is used to interpret the way in which the bytes are stored in the memory. It doesn't dictate how bytes are to be accessed if you are directly referring the variable without any pointer operations.

Which means, the below program will produce same result irrespective of the endianess of the platform.

int main(void)
{
    int num = 0xDEADBEEF;
    int mask = 0xF0000000;

    printf("SET = %X\n", (unsigned int) (num | mask));
    return 0;
}
Santosh A
  • 5,173
  • 27
  • 37