decimal constants

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    #1

    decimal constants

    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 ....

  • Vladimir S. Oka

    #2
    Re: decimal constants

    junky_fellow@ya hoo.co.in wrote:[color=blue]
    > 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 ....
    >[/color]

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

    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.

    Comment

    • Vladimir S. Oka

      #3
      Re: decimal constants

      junky_fellow@ya hoo.co.in wrote:[color=blue]
      > 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 ....
      >[/color]

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

      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.

      Comment

      Working...