Macro for setting MSB - Intended to work on both Little andBig-endian machines

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 1LL(LL - long long - to force 1 to be stored in a multiple memory resource(say register) to hold the value 1) in a variable in my program, it will be accessed as a multi-byte valueand hence 1 will be stored in the LSB of most-significant-byte of the memory resource(say register) and not in the LSB of least-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;
}


Cheers,
Raghavan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top