By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

what is the difference between two assignments

P: n/a
Hi,

Is there any difference between

header = hdr_type & 0x80 ; and

header = !!(hdr_type & 0x80);

I have seen the second kind of assignment in some places.Does it have
any special reason ?

Regards
rupesh

Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
>Is there any difference between

header = hdr_type & 0x80 ; and

header = !!(hdr_type & 0x80);
Yes. For the first one, header might have the values 0 or 0x80.
For the second one, header might have the values 0 or 1.

Sometimes people prefer to have "boolean" values (regardless of
what type it's stored in) limited to the values 0 or 1.
I have seen the second kind of assignment in some places.Does it have
any special reason ?


The reason does not qualify as special under the legislation
controlling intelligence agencies (oxymoron!) in the United States.

Gordon L. Burditt
Nov 17 '05 #2

P: n/a

ru********@rediffmail.com wrote:
Hi,

Is there any difference between

header = hdr_type & 0x80 ; and

header = !!(hdr_type & 0x80);

I have seen the second kind of assignment in some places.Does it have
any special reason ?

Regards
rupesh


It is making the result 0 or 1 instead of 0 or 0x80,
perhaps because of some desire for the result to be
like a C99 _Bool.

-David

Nov 17 '05 #3

P: n/a
ru********@rediffmail.com wrote:
Hi,

Is there any difference between

header = hdr_type & 0x80 ; and
This makes header either 0 or 0x80.
header = !!(hdr_type & 0x80);
This makes header either 0 or 1, since '!' yields either 0 or 1. This
can be confusing because it ignores a well-known law of boolean logic:
!!a = a; double negation has no effect and can be removed. It doesn't
work that way here because C's notion of a boolean is not that of a
two-value type, so !! does have effect and actually "converts" its
argument to a one-bit value. Its value as a boolean is still unchanged,
though: for a boolean test it doesn't matter whether you test on 1 or 0x80.
I have seen the second kind of assignment in some places.Does it have
any special reason ?

Yes, it's an (in my opinion) less intuitive way of writing

header = hdr_type & 0x80 ? 1 : 0;

Of course, if you only use 'header' as a boolean, in tests, there is no
need for this in the first place and you can just use 'hdr_type & 0x80'.

S.
Nov 17 '05 #4

P: n/a
ru********@rediffmail.com wrote:
!!(hdr_type & 0x80)


My prefered way of writing that is:

(hdr_type & 0x80) != 0

--
pete
Nov 17 '05 #5

P: n/a
ru********@rediffmail.com a écrit :
Hi,

Is there any difference between

header = hdr_type & 0x80 ; and
Returns 0 or 0x80
header = !!(hdr_type & 0x80);
Returns 0 or 1
I have seen the second kind of assignment in some places.Does it have
any special reason ?


Yes.

--
A+

Emmanuel Delahaye
Nov 17 '05 #6

P: n/a
Skarmander wrote:
ru********@rediffmail.com wrote:

Is there any difference between

header = hdr_type & 0x80 ; and

This makes header either 0 or 0x80.
header = !!(hdr_type & 0x80);

This makes header either 0 or 1, since '!' yields either 0 or 1.
This can be confusing because it ignores a well-known law
of boolean logic. Its value as a boolean is still unchanged,
though: for a boolean test it doesn't matter whether you test
on 1 or 0x80.


If 'header' is a signed char, then the first statement is not
well-defined (it assigns an out-of-range value to a signed
integral type). That's the most common reason for using "!!",
in my own code anyway.

Nov 17 '05 #7

P: n/a
"David Resnick" <ln********@gmail.com> wrote:
#
# ru********@rediffmail.com wrote:
# > Hi,
# >
# > Is there any difference between
# >
# > header = hdr_type & 0x80 ; and
# >
# > header = !!(hdr_type & 0x80);
# >
# > I have seen the second kind of assignment in some places.Does it have
# > any special reason ?
# >
# > Regards
# > rupesh
#
# It is making the result 0 or 1 instead of 0 or 0x80,
# perhaps because of some desire for the result to be
# like a C99 _Bool.

Or assigning to a struct {...; int field:1; ...} member.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no respect for people with no shopping agenda.
Nov 18 '05 #8

P: n/a
On 2005-11-17, ru********@rediffmail.com <ru********@rediffmail.com> wrote:
Hi,

Is there any difference between

header = hdr_type & 0x80 ; and

header = !!(hdr_type & 0x80);

I have seen the second kind of assignment in some places.Does it have
any special reason ?


The latter forces it to 0 or 1.
Nov 18 '05 #9

P: n/a
Skarmander wrote:
ru********@rediffmail.com wrote:
Hi,

Is there any difference between

header = hdr_type & 0x80 ; and

This makes header either 0 or 0x80.
header = !!(hdr_type & 0x80);

This makes header either 0 or 1, since '!' yields either 0 or 1. This
can be confusing because it ignores a well-known law of boolean logic:
!!a = a; double negation has no effect and can be removed. It doesn't
work that way here because C's notion of a boolean is not that of a
two-value type, so !! does have effect and actually "converts" its
argument to a one-bit value. Its value as a boolean is still unchanged,
though: for a boolean test it doesn't matter whether you test on 1 or 0x80.
I have seen the second kind of assignment in some places.Does it have
any special reason ?

Yes, it's an (in my opinion) less intuitive way of writing

header = hdr_type & 0x80 ? 1 : 0;


Which is in turn a (in my opinion) less intuitive way
of writing

header = (hdr_type & 0x80) != 0;

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 18 '05 #10

P: n/a
Eric Sosman wrote:
Skarmander wrote:
ru********@rediffmail.com wrote:
<snip>
header = !!(hdr_type & 0x80);
<snip> I have seen the second kind of assignment in some places.Does it have
any special reason ?

Yes, it's an (in my opinion) less intuitive way of writing

header = hdr_type & 0x80 ? 1 : 0;

Which is in turn a (in my opinion) less intuitive way
of writing

header = (hdr_type & 0x80) != 0;


Depends. If 'header' is a boolean, yes. If 'header' is a bit value, I'd
use the ternary operator. The statements are, of course, equivalent.

S.
Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.