Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Macro for setting MSB - Intended to work on both Little andBig-endian machines

Reply
Thread Tools

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

 
 
Myth__Buster
Guest
Posts: n/a
 
      03-26-2013
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 multiple 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 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for setting MSB - Intended to work on both Little and Bigendian machines Myth__Buster C Programming 8 03-27-2013 04:01 AM
Macro for setting MSB - Intended to work on both Little andBig-endian machines Myth__Buster C Programming 16 03-26-2013 10:01 PM
Macro for setting MSB - Intended to work on both Little andBig-endian machines Myth__Buster C Programming 1 03-26-2013 12:44 PM
Macro for setting MSB - Intended to work on both Little andBig-endian machines Myth__Buster C Programming 0 03-26-2013 08:00 AM
Macro for setting MSB - Intended to work on both Little andBig-endian machines Myth__Buster C Programming 0 03-26-2013 07:56 AM



Advertisments