Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Macro for setting MSB - Intended to work on both Little andBig-endian machines (http://www.velocityreviews.com/forums/t959067-macro-for-setting-msb-intended-to-work-on-both-little-andbig-endian-machines.html)

Myth__Buster 03-26-2013 07:56 AM

Macro for setting MSB - Intended to work on both Little andBig-endian machines
 
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



All times are GMT. The time now is 12:40 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.