M
Myth__Buster
Hi All,
Here is my attempt for setting the MSB of an integer depending upon whetherthe underlying machine is Little or Big-endian. Any comments/suggestions/views are appreciated.
Here I have assumed though I don't store the 1ULL(LL - long long - to force1 to be stored in a multi-byte memory resource(say register) to hold the value 1) in a variable in my program, it will be accessed as a multi-byte value and hence 1 will be stored in the LSB of most-significant-byte of the value stored in a multi-byte memory resource(register) and not in the LSB ofleast-significant-byte of that resource. Please let me know if this is correct.
<Code>
#include <stdio.h>
#include <limits.h>
#define LSET_MSB(x) ((x) = (x) | 1ULL << (sizeof(x) * CHAR_BIT - 1))
#define BSET_MSB(x) ((x) = (x) | 1ULL << (CHAR_BIT - 1))
#define LIITE_ENDIAN (1ULL & 1)
int main(void)
{
unsigned long long int x = 1;
printf("x : %llu\n", x);
printf("x : %#llx\n", x);
if ( LIITE_ENDIAN )
{
printf("Little\n");
LSET_MSB(x);
}
else
{
printf("Big\n");
BSET_MSB(x);
}
printf("x : %llu\n", x);
printf("x : %#llx\n", x);
return 0;
}
</code>
<OutputOnMyMachine>
x : 1
x : 0x1
Little
x : 9223372036854775809
x : 0x8000000000000001
</OutputOnMyMachine>
Cheers,
Raghavan
Here is my attempt for setting the MSB of an integer depending upon whetherthe underlying machine is Little or Big-endian. Any comments/suggestions/views are appreciated.
Here I have assumed though I don't store the 1ULL(LL - long long - to force1 to be stored in a multi-byte memory resource(say register) to hold the value 1) in a variable in my program, it will be accessed as a multi-byte value and hence 1 will be stored in the LSB of most-significant-byte of the value stored in a multi-byte memory resource(register) and not in the LSB ofleast-significant-byte of that resource. Please let me know if this is correct.
<Code>
#include <stdio.h>
#include <limits.h>
#define LSET_MSB(x) ((x) = (x) | 1ULL << (sizeof(x) * CHAR_BIT - 1))
#define BSET_MSB(x) ((x) = (x) | 1ULL << (CHAR_BIT - 1))
#define LIITE_ENDIAN (1ULL & 1)
int main(void)
{
unsigned long long int x = 1;
printf("x : %llu\n", x);
printf("x : %#llx\n", x);
if ( LIITE_ENDIAN )
{
printf("Little\n");
LSET_MSB(x);
}
else
{
printf("Big\n");
BSET_MSB(x);
}
printf("x : %llu\n", x);
printf("x : %#llx\n", x);
return 0;
}
</code>
<OutputOnMyMachine>
x : 1
x : 0x1
Little
x : 9223372036854775809
x : 0x8000000000000001
</OutputOnMyMachine>
Cheers,
Raghavan