Re: bitwise operation (&)

Discussion in 'C Programming' started by Ben Bacarisse, May 24, 2010.

  1. "hzphb" <> writes:

    > this mail should have been seen in comp.lang.c.noderated two days ago.
    > now,i can't find it.so i post the mail in here and want to find something
    > different.
    > //--------------
    > i want to present one example of '&':
    >
    > #include "stdio.h"


    #include <stdio.h>

    The ""s should be used for your own header files. Standard ones should
    be in <>s.

    > void main()


    int main() is better and int main(void) is better yet.

    > {
    > int ori_data=2010;
    > int count =0;
    >
    > for(;ori_data;)


    Using only the middle part of the for statement makes this equivalent to

    while (ori_data)

    and your readers will find that more immediately obvious. There's no
    gain from having the extra noise of a for statement.

    > {
    > ori_data = ori_data & (ori_data - 1);


    In general you should use unsigned ints when doing bit operations. This
    specific example works fine, but rather than worry about exactly what
    operations do and do not work with signed ints, just get into the habit
    of using unsigned ints when you can.

    If you like exercises, try to think of cases where your code won't work.

    > count++;
    > }
    > printf("%d \n",count);
    > }
    >
    > now,i'd like to learn something from you about this example.
    > //-----------------------


    OK. This method of counting set bits was first published by Peter
    Wegner in 1960 and popularised by its appearance in K&R -- the
    definitive text about C programming. It is particularly efficient when
    there are only a few bits set.

    --
    Ben.
     
    Ben Bacarisse, May 24, 2010
    #1
    1. Advertising

  2. On 24 May, 03:20, Ben Bacarisse <> wrote:
    > "hzphb" <> writes:


    <snip>

    > int main() is better and int main(void) is better yet.
    >
    > > {
    > >  int ori_data=2010;
    > >  int count =0;

    [...]
    >   while (ori_data)

    [...]
    > >  {
    > >   ori_data = ori_data & (ori_data - 1);


    you can slightly shorten this by using the &= operator

    ori_data &= ori_data - 1;

    > In general you should use unsigned ints when doing bit operations.


    yes.

    > This
    > specific example works fine, but rather than worry about exactly what
    > operations do and do not work with signed ints, just get into the habit
    > of using unsigned ints when you can.


    why? I normally use use signed quantitites unless I can see a good
    reason not to. A good reason is because bit level operations are being
    used (& | ^ ~ >> <<). Unsigned quantitiies have their own problems.

    <snip>

    --
    "The Dinosaurs have come and gone,
    we Theriodonts remain"
     
    Nick Keighley, May 24, 2010
    #2
    1. Advertising

  3. "hzphb" <> wrote:
    [message snipped by OP]
    > Thank you very much for your directions.
    >
    > You have a excellent reply for me.
    > In the reply,i can learn something valuable.


    James Waldby also gave you an excellent reply. It stated
    problems with your code clearly; he explained what it was
    doing, and he went to so far as to offer you an extremely
    useful resource, namely the FAQ. You should be using that
    for learning C programming in general, as well as reading
    prior to posting to usenet.

    --
    Peter
     
    Peter Nilsson, May 24, 2010
    #3
  4. Nick Keighley <> wrote:
    > Ben Bacarisse <> wrote:
    > > In general you should use unsigned ints when doing bit
    > > operations.

    >
    > yes.
    >
    > > This specific example works fine, but rather than worry
    > > about exactly what operations do and do not work with
    > > signed ints, just get into the habit of using unsigned
    > > ints when you can.

    >
    > why? I normally use use signed quantitites unless I can see
    > a good reason not to.


    In other words, you normally do the wrong thing until something
    bad happens.

    > A good reason is because bit level operations are being
    > used (& | ^ ~ >> <<).


    Precisely.

    > Unsigned quantitiies have their own problems.


    Pure binary representation is a problem for bit level
    operations? Hammers have their problems, but they're
    still the perfect thing for nails. ;)

    --
    Peter
     
    Peter Nilsson, May 24, 2010
    #4
  5. On 24 May, 08:23, Peter Nilsson <> wrote:
    > Nick Keighley <> wrote:
    > > Ben Bacarisse <> wrote:


    > > > In general you should use unsigned ints when doing bit
    > > > operations.

    >
    > > yes.

    >
    > > > This specific example works fine, but rather than worry
    > > > about exactly what operations do and do not work with
    > > > signed ints, just get into the habit of using unsigned
    > > > ints when you can.

    >
    > > why? I normally use use signed quantitites unless I can see
    > > a good reason not to.

    >
    > In other words, you normally do the wrong thing until something
    > bad happens.


    I'd say exactly the same about you

    > > A good reason is because bit level operations are being
    > > used (& | ^ ~ >> <<).

    >
    > Precisely.


    ? what? Most of my code doesn't use bit-level operations so it doesn't
    use unsigned types.

    > > Unsigned quantitiies have their own problems.

    >
    > Pure binary representation is a problem for bit level
    > operations?


    no. I've no argument with using unsigned for bit level stuff. Did you
    read what i wrote?

    > Hammers have their problems, but they're
    > still the perfect thing for nails. ;)



    --

    In a profession plagued by, "when all you have is a hammer, everything
    looks like a nail," we get really excited when someone is able to come
    along and prove that everything really *is* a nail if lambda is the
    hammer.
    B.R.Lewis (comp.lang.scheme)
     
    Nick Keighley, May 24, 2010
    #5
  6. Nick Keighley <> writes:

    > On 24 May, 03:20, Ben Bacarisse <> wrote:
    >> "hzphb" <> writes:

    <snip>
    >> >  int ori_data=2010;

    <snip>
    >> >   ori_data = ori_data & (ori_data - 1);

    <snip>
    >> In general you should use unsigned ints when doing bit operations.

    >
    > yes.
    >
    >> This
    >> specific example works fine, but rather than worry about exactly what
    >> operations do and do not work with signed ints, just get into the habit
    >> of using unsigned ints when you can.

    >
    > why? I normally use use signed quantitites unless I can see a good
    > reason not to. A good reason is because bit level operations are being
    > used (& | ^ ~ >> <<). Unsigned quantitiies have their own problems.


    We don't disagree. The two sentences were to be read together: use
    unsigned ints when doing this sort of thing rather than trying to work
    out if what you are doing can be done with signed ints.

    I should have been clearer. The meaning of the second sentence depends
    on what the paragraph is about. Is it about choosing types or
    using unsigned ints for this sort of operation?

    However, it's worth noting that your idea of "low-level operations" is
    not always sufficient. In this case, its the '- 1' that's the problem.
    In effect it is being used as "bit operation" not an arithmetic one.

    One thing is certain: the OP will not be misled by my wording now!

    <snip>
    --
    Ben.
     
    Ben Bacarisse, May 24, 2010
    #6
  7. On 24 May, 11:22, Ben Bacarisse <> wrote:
    > Nick Keighley <> writes:
    > > On 24 May, 03:20, Ben Bacarisse <> wrote:
    > >> "hzphb" <> writes:


    > >> >  int ori_data=2010;


    > >> >   ori_data = ori_data & (ori_data - 1);


    > >> In general you should use unsigned ints when doing bit operations.

    >
    > > yes.

    >
    > >> This
    > >> specific example works fine, but rather than worry about exactly what
    > >> operations do and do not work with signed ints, just get into the habit
    > >> of using unsigned ints when you can.


    ah sorry, I read that as "use unsigned ints" in preference to "signed
    ints"- which isn't really what you said.



    > > why? I normally use use signed quantitites unless I can see a good
    > > reason not to. A good reason is because bit level operations are being
    > > used (& | ^ ~ >> <<). Unsigned quantitiies have their own problems.

    >
    > We don't disagree.  The two sentences were to be read together: use
    > unsigned ints when doing this sort of thing rather than trying to work
    > out if what you are doing can be done with signed ints.
    >
    > I should have been clearer.  The meaning of the second sentence depends
    > on what the paragraph is about.  Is it about choosing types or
    > using unsigned ints for this sort of operation?
    >
    > However, it's worth noting that your idea of "low-level operations" is
    > not always sufficient.  In this case, its the '- 1' that's the problem.
    > In effect it is being used as "bit operation" not an arithmetic one.
    >
    > One thing is certain: the OP will not be misled by my wording now!
     
    Nick Keighley, May 24, 2010
    #7
    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. Pasquale Imbemba

    Bitwise Operation

    Pasquale Imbemba, May 6, 2004, in forum: Java
    Replies:
    2
    Views:
    546
    Roedy Green
    May 7, 2004
  2. biswaranjan.rath

    bitwise AND operation in xslt

    biswaranjan.rath, May 8, 2006, in forum: XML
    Replies:
    3
    Views:
    4,899
    shaunroe
    Nov 12, 2008
  3. Patrick Hoonhout

    bitwise operation...

    Patrick Hoonhout, Aug 27, 2003, in forum: C Programming
    Replies:
    13
    Views:
    667
    Alan Balmer
    Aug 28, 2003
  4. Magix

    Bitwise operation

    Magix, Jul 30, 2004, in forum: C Programming
    Replies:
    6
    Views:
    736
    Joe Wright
    Jul 31, 2004
  5. Magix

    Bitwise Operation

    Magix, Oct 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    557
    Dimension
    Oct 15, 2004
Loading...

Share This Page