decimal constants

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jan 16, 2006.

  1. Guest

    Consider the following piece of code:
    Note: The size of long is 64 bits on my processor.

    #define MYCONSTANT 0x80000000UL

    int main(void)
    {
    unsigned long i = 0x1000;

    i &= ~MYCONSTANT;


    }

    My question is which is better,
    #define MYCONSTANT 0x80000000UL
    or
    #define MYCONSTANT 0x80000000L


    Thanx for any help in advance ....
    , Jan 16, 2006
    #1
    1. Advertising

  2. wrote:
    > Consider the following piece of code:
    > Note: The size of long is 64 bits on my processor.
    >
    > #define MYCONSTANT 0x80000000UL
    >
    > int main(void)
    > {
    > unsigned long i = 0x1000;
    >
    > i &= ~MYCONSTANT;
    >
    >
    > }
    >
    > My question is which is better,
    > #define MYCONSTANT 0x80000000UL
    > or
    > #define MYCONSTANT 0x80000000L
    >
    >
    > Thanx for any help in advance ....
    >


    Size of long being 64 bits you'd get the same result either way
    (0x80000000 being the same as 0x0000000080000000).

    However, if you declare i as unsigned long, then MYCONSTANT is most
    naturally a UL as well, especially if you don't want to be bitten
    porting to a machine where long is shorter, and also especially if
    MYCONSTANT is going to be used non-negated as well.

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 16, 2006
    #2
    1. Advertising

  3. wrote:
    > Consider the following piece of code:
    > Note: The size of long is 64 bits on my processor.
    >
    > #define MYCONSTANT 0x80000000UL
    >
    > int main(void)
    > {
    > unsigned long i = 0x1000;
    >
    > i &= ~MYCONSTANT;
    >
    >
    > }
    >
    > My question is which is better,
    > #define MYCONSTANT 0x80000000UL
    > or
    > #define MYCONSTANT 0x80000000L
    >
    >
    > Thanx for any help in advance ....
    >


    Size of long being 64 bits you'd get the same result either way
    (0x80000000 being the same as 0x0000000080000000).

    However, if you declare i as unsigned long, then MYCONSTANT is most
    naturally a UL as well, especially if you don't want to be bitten
    porting to a machine where long is shorter, and also especially if
    MYCONSTANT is going to be used non-negated as well.

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 16, 2006
    #3
    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. Ven
    Replies:
    3
    Views:
    1,300
  2. Gilbert Fine
    Replies:
    8
    Views:
    882
    Zentrader
    Aug 1, 2007
  3. Vitaliy
    Replies:
    1
    Views:
    458
    Peter Otten
    May 29, 2008
  4. valpa
    Replies:
    11
    Views:
    1,486
    Steven D'Aprano
    Mar 24, 2009
  5. Replies:
    0
    Views:
    271
Loading...

Share This Page