what is the difference between two assignments

R

rupesh_533

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
 
G

Gordon Burditt

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
 
D

David Resnick

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
 
S

Skarmander

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

Old Wolf

Skarmander said:
This makes header either 0 or 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.
 
S

SM Ryan

#
# (e-mail address removed) 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.
 
J

Jordan Abel

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

Eric Sosman

Skarmander said:
This makes header either 0 or 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.

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;
 
S

Skarmander

Eric said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top