Using bitshift in assigning a number to a variable?

Discussion in 'C Programming' started by bollod, Dec 25, 2003.

  1. bollod

    bollod Guest

    Is there any benefit to declaring a variable:

    x = (1<<12);

    instead of:

    x = 4096;

    (besides the geek appeal of course ;-) )

    I have seen these kind of declarations come up in some code I have been
    fiddling with and was wondering if there was any tangible benefit, (I have
    also seen stuff declared as hex, is there any benefit to that either).

    ---
    bollod
    bollod, Dec 25, 2003
    #1
    1. Advertising

  2. bollod

    bollod Guest

    On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:

    > bollod wrote:
    >>
    >> Is there any benefit to declaring a variable:
    >>
    >> x = (1<<12);
    >>
    >> instead of:
    >>
    >> x = 4096;
    >>
    >> (besides the geek appeal of course ;-) )
    >>
    >> I have seen these kind of declarations come up
    >> in some code I have been
    >> fiddling with and was wondering if there was any tangible benefit,
    >> (I have also seen stuff declared as hex,
    >> is there any benefit to that either).

    >
    > It's easier to visualise the binary representation
    > of the values that way,
    > which is especially good if your numbers are masks.


    That makes sense.

    What would you do if you needed to create the mask '10101010' (besides
    foo=170 or foo=0xA4)?

    ---
    bollod
    bollod, Dec 25, 2003
    #2
    1. Advertising

  3. bollod

    pete Guest

    bollod wrote:
    >
    > Is there any benefit to declaring a variable:
    >
    > x = (1<<12);
    >
    > instead of:
    >
    > x = 4096;
    >
    > (besides the geek appeal of course ;-) )
    >
    > I have seen these kind of declarations come up
    > in some code I have been
    > fiddling with and was wondering if there was any tangible benefit,
    > (I have also seen stuff declared as hex,
    > is there any benefit to that either).


    It's easier to visualise the binary representation
    of the values that way,
    which is especially good if your numbers are masks.

    --
    pete
    pete, Dec 26, 2003
    #3
  4. bollod

    pete Guest

    bollod wrote:
    >
    > On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
    >
    > > bollod wrote:
    > >>
    > >> Is there any benefit to declaring a variable:
    > >>
    > >> x = (1<<12);
    > >>
    > >> instead of:
    > >>
    > >> x = 4096;
    > >>
    > >> (besides the geek appeal of course ;-) )
    > >>
    > >> I have seen these kind of declarations come up
    > >> in some code I have been
    > >> fiddling with and was wondering if there was any tangible benefit,
    > >> (I have also seen stuff declared as hex,
    > >> is there any benefit to that either).

    > >
    > > It's easier to visualise the binary representation
    > > of the values that way,
    > > which is especially good if your numbers are masks.

    >
    > That makes sense.
    >
    > What would you do if you needed to create the mask '10101010' (besides
    > foo=170 or foo=0xA4)?


    '10101010' looks more like 0xaa to me.

    The shift notation is better
    when the right operand of the shift operator
    is an enum or a macro identifier which indicates which flag
    the mask applies to.

    --
    pete
    pete, Dec 26, 2003
    #4
  5. bollod wrote:
    > What would you do if you needed to create the mask '10101010' (besides
    > foo=170 or foo=0xA4)?


    I would write
    #define SOME_MASK (1<<7 | 1<<5 | 1<<3 | 1<<1)
    or
    const int SOME_MASK=1<<7 | 1<<5 | 1<<3 | 1<<1;

    --
    Robert Bachmann, Dec 26, 2003
    #5
  6. in comp.lang.c i read:

    >What would you do if you needed to create the mask '10101010' (besides
    >foo=170 or foo=0xA4)?


    correction: foo=0xAA. also foo=0252 and foo=strtol("10101010", 0, 2),
    though the later cannot be used at file scope.

    --
    a signature
    those who know me have no need of my name, Dec 26, 2003
    #6
  7. On Thu, 25 Dec 2003, bollod wrote:

    > Is there any benefit to declaring a variable:
    >
    > x = (1<<12);
    >
    > instead of:
    >
    > x = 4096;
    >
    > (besides the geek appeal of course ;-) )
    >
    > I have seen these kind of declarations come up in some code I have been
    > fiddling with and was wondering if there was any tangible benefit, (I have
    > also seen stuff declared as hex, is there any benefit to that either).


    I'll answer the easier question first, there is a benefit to hexidecimal
    notation. When I am doing bit twiddling it is easier to visualize the bit
    pattern what using hexidecimal notation. One digit in hexidecimal notation
    is exactly 4 bits.

    The harder question is why use (1<<12) rather than 4096. If they used
    0x1000 it would be more obvious. I'd guess that 99% of the time this is
    just more readable. Another possible option is that someone was tuning the
    code and found a 'trick' for a particular processor/compiler.

    It is not impossible that a compiler will take:

    x = (1<<12);

    and optimize it to a STORE IMMEDIATE 1 and SHIFT opcode. The cycle count
    for this might be faster than trying to assign 4096. This is a stretch but
    not impossible. Mind you, I'd put a comment next to the code in case a
    revision in the compiler or a port to a different architecture made this
    tweek disappear.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
    Darrell Grainger, Dec 27, 2003
    #7
  8. "bollod" <> wrote in message
    news:p...
    > On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
    >
    > > bollod wrote:
    > >>
    > >> Is there any benefit to declaring a variable:
    > >>
    > >> x = (1<<12);
    > >>
    > >> instead of:
    > >>
    > >> x = 4096;
    > >>
    > >> (besides the geek appeal of course ;-) )
    > >>
    > >> I have seen these kind of declarations come up
    > >> in some code I have been
    > >> fiddling with and was wondering if there was any tangible benefit,
    > >> (I have also seen stuff declared as hex,
    > >> is there any benefit to that either).

    > >
    > > It's easier to visualise the binary representation
    > > of the values that way,
    > > which is especially good if your numbers are masks.

    >
    > That makes sense.
    >
    > What would you do if you needed to create the mask '10101010' (besides
    > foo=170 or foo=0xA4)?
    >
    > ---
    > bollod


    x=2<<6|2<<4|2<<2|2;

    or

    x=10<<4|10;

    or

    x=5<<5|5<<1;

    or

    x=85<<1;


    But now its just getting silly.....

    Sean
    Sean Kenwrick, Dec 27, 2003
    #8
  9. bollod wrote:

    > On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
    >
    >> It's easier to visualise the binary representation
    >> of the values that way,
    >> which is especially good if your numbers are masks.

    >
    > That makes sense.
    >
    > What would you do if you needed to create the mask '10101010' (besides
    > foo=170 or foo=0xA4)?


    #define l )*2+1
    #define O )*2
    #define b1 ((((((((0

    unsigned char foo = b1 l O l O l O l O ;

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Dec 27, 2003
    #9
  10. bollod wrote:
    > Is there any benefit to declaring a variable:
    >
    > x = (1<<12);
    >
    > instead of:
    >
    > x = 4096;
    >
    > (besides the geek appeal of course ;-) )

    If x is used as a "common variable" I can see no benefit.

    But if x is used as some kind of bit-mask you got the benefit of
    immediately seeing that the value of x is 1000000000000 in binary notation.

    As (1<<n) can be read as 1 followed by n zero digits in binary notation.
    For example: 1<<3 is 1000 in binary.

    > (I have also seen stuff declared as hex, is there any benefit to that either).


    Hexadecimal notation is also very common when using bit-masks.
    As Darrell Grainger already said:
    | One digit in hexidecimal notation
    | is exactly 4 bits.

    So if you familiar with hexadecimal notation it's easy to see that
    0x1000 (4096) is 1000000000000.

    I also have seen code with bit-masks in octal notation (one digit in
    octal notation represents 3 bits).

    HTH,
    Robert

    --
    Robert Bachmann, Dec 27, 2003
    #10
  11. bollod

    pete Guest

    Robert Bachmann wrote:
    >
    > bollod wrote:
    > > What would you do if you needed to create the mask '10101010' (besides
    > > foo=170 or foo=0xA4)?

    >
    > I would write
    > #define SOME_MASK (1<<7 | 1<<5 | 1<<3 | 1<<1)


    Out of all of the various replies posted,
    that form, shifts and bitwise ors,
    is similar to macros that I've actually used.

    --
    pete
    pete, Dec 27, 2003
    #11
  12. ----- Original Message -----
    From: "Richard Heathfield" <>
    Newsgroups: comp.lang.c
    Sent: 27 December 2003 11:12
    Subject: Re: Using bitshift in assigning a number to a variable?


    > bollod wrote:
    >
    > > On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
    > >
    > >> It's easier to visualise the binary representation
    > >> of the values that way,
    > >> which is especially good if your numbers are masks.

    > >
    > > That makes sense.
    > >
    > > What would you do if you needed to create the mask '10101010' (besides
    > > foo=170 or foo=0xA4)?

    >
    > #define l )*2+1
    > #define O )*2
    > #define b1 ((((((((0
    >
    > unsigned char foo = b1 l O l O l O l O ;
    >


    Hey this is pretty neat way of representing binary in your 'C' code. I
    would generalise it a bit further by allowing 16 and 32 bit values as well,
    something like:

    #define bin32bit ((((((((((((((((((((((((((((((((0
    #define bin16bit ((((((((((((((((0
    #define bin8bit ((((((((0

    #define O )<<1
    #define I )<<1+1


    Then

    x=bin8bit I O I O I O I O

    or

    x = bin16bit I O I O I O I O I O I O I O I O

    nice!

    Sean
    Sean Kenwrick, Dec 27, 2003
    #12
  13. Sean Kenwrick wrote:

    > ----- Original Message -----
    > From: "Richard Heathfield" <>
    > Newsgroups: comp.lang.c
    > Sent: 27 December 2003 11:12
    > Subject: Re: Using bitshift in assigning a number to a variable?
    >
    >
    >> bollod wrote:
    >>
    >> > On Fri, 26 Dec 2003 04:03:30 +0000, pete wrote:
    >> >
    >> >> It's easier to visualise the binary representation
    >> >> of the values that way,
    >> >> which is especially good if your numbers are masks.
    >> >
    >> > That makes sense.
    >> >
    >> > What would you do if you needed to create the mask '10101010' (besides
    >> > foo=170 or foo=0xA4)?

    >>
    >> #define l )*2+1
    >> #define O )*2
    >> #define b1 ((((((((0
    >>
    >> unsigned char foo = b1 l O l O l O l O ;
    >>

    >
    > Hey this is pretty neat way of representing binary in your 'C' code. I
    > would generalise it a bit further by allowing 16 and 32 bit values as
    > well,


    Yes, that's why I called it b1 rather than b - to allow for b2 and b4 (and,
    if you have bigger integral types, b8 and so on as well).

    See "Expert C Programming (Deep C Secrets)" by Peter van der Linden, p203,
    for the original idea. All I did was hack the names.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Dec 27, 2003
    #13
    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. Ryan
    Replies:
    4
    Views:
    2,962
    Rusty Wright
    Feb 14, 2005
  2. Replies:
    8
    Views:
    319
    Larry I Smith
    Feb 10, 2006
  3. Replies:
    6
    Views:
    367
    Grant Edwards
    May 24, 2007
  4. Serve Laurijssen

    bitshift operator problem

    Serve Laurijssen, Sep 5, 2008, in forum: C Programming
    Replies:
    13
    Views:
    597
    Ben Bacarisse
    Sep 6, 2008
  5. Bill Cunningham

    bitshift

    Bill Cunningham, Nov 8, 2011, in forum: C Programming
    Replies:
    1
    Views:
    275
    Lew Pitcher
    Nov 9, 2011
Loading...

Share This Page