Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: bitwise operation (&)

Reply
Thread Tools

Re: bitwise operation (&)

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-24-2010
"hzphb" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      05-24-2010
On 24 May, 03:20, Ben Bacarisse <(E-Mail Removed)> wrote:
> "hzphb" <(E-Mail Removed)> 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"
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      05-24-2010
"hzphb" <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      05-24-2010
Nick Keighley <(E-Mail Removed)> wrote:
> Ben Bacarisse <(E-Mail Removed)> 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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-24-2010
On 24 May, 08:23, Peter Nilsson <(E-Mail Removed)> wrote:
> Nick Keighley <(E-Mail Removed)> wrote:
> > Ben Bacarisse <(E-Mail Removed)> 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)
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-24-2010
Nick Keighley <(E-Mail Removed)> writes:

> On 24 May, 03:20, Ben Bacarisse <(E-Mail Removed)> wrote:
>> "hzphb" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-24-2010
On 24 May, 11:22, Ben Bacarisse <(E-Mail Removed)> wrote:
> Nick Keighley <(E-Mail Removed)> writes:
> > On 24 May, 03:20, Ben Bacarisse <(E-Mail Removed)> wrote:
> >> "hzphb" <(E-Mail Removed)> 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!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitwise AND operation in xslt biswaranjan.rath XML 3 11-12-2008 03:14 PM
Bitwise Operation Magix C Programming 5 10-15-2004 04:45 PM
Bitwise operation Magix C Programming 6 07-30-2004 11:55 PM
Bitwise Operation Pasquale Imbemba Java 2 05-06-2004 11:19 PM
bitwise operation... Patrick Hoonhout C Programming 13 08-28-2003 08:02 PM



Advertisments