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

Discussion in 'C Programming' started by Myth__Buster, Mar 26, 2013.

  1. Myth__Buster

    Myth__Buster Guest

    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>

    <op>
    x : 1
    x : 0x1
    Little
    x : 9223372036854775809
    x : 0x8000000000000001
    </op>

    Cheers,
    Raghavan
    Myth__Buster, Mar 26, 2013
    #1
    1. Advertising

  2. Myth__Buster

    Eric Sosman Guest

    Re: Macro for setting MSB - Intended to work on both Little and Big-endianmachines

    On 3/26/2013 4:02 AM, Myth__Buster wrote:
    > Hi All,
    >
    > Here is my attempt for setting the MSB of an integer depending upon whether the 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 force 1 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 of least-significant-byte of that resource. Please let me know if this is correct.


    It's not correct.

    > #define LIITE_ENDIAN (1ULL & 1)
    > [...]
    > if ( LIITE_ENDIAN )


    After macro substitution, this is

    if ( ( 1ULL & 1 ) )

    The "usual arithmetic conversions" are performed on the operands of `&',
    converting the constant `1' from `int' to `unsigned long long', yielding
    the equivalent of

    if ( ( 1ULL & 1ULL ) )

    The `&' is evaluated on the two `unsigned long long' operands, giving

    if ( ( 1ULL ) )

    The inner parentheses serve no further purpose, so now you have

    if ( 1ULL )

    .... which always takes its "true" branch, regardless of whether
    the machine is big-endian, little-endian, middle-endian, or
    scrambled-eggian.

    --
    Eric Sosman
    d
    Eric Sosman, Mar 26, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Myth__Buster
    Replies:
    8
    Views:
    229
    Correador UK
    Mar 27, 2013
  2. Myth__Buster
    Replies:
    0
    Views:
    157
    Myth__Buster
    Mar 26, 2013
  3. Myth__Buster
    Replies:
    0
    Views:
    165
    Myth__Buster
    Mar 26, 2013
  4. Myth__Buster
    Replies:
    0
    Views:
    166
    Myth__Buster
    Mar 26, 2013
  5. Myth__Buster
    Replies:
    16
    Views:
    373
    glen herrmannsfeldt
    Mar 26, 2013
Loading...

Share This Page