473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

what is the difference between two assignments

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
10 1362
>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

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
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
ru********@rediffmail.com wrote:
!!(hdr_type & 0x80)


My prefered way of writing that is:

(hdr_type & 0x80) != 0

--
pete
Nov 17 '05 #5
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
4
by: Jacek Generowicz | last post by:
I am dealing with an application which reads in configurations by calling (a wrapper around) execfile. Any configuration file may itself execfile other configuration files in the same manner. I...
3
by: bearophileHUGS | last post by:
The current version of ShedSkin (http://shedskin.sourceforge.net/ experimental Python to C++ compiler) is rather alpha, and the development work is focused on debugging and implementing some more...
21
by: Paul Steckler | last post by:
Here's some code that's giving me differing results, depending on the compiler. typedef foo { int A,B; } FOO; int main() {
19
by: Jason | last post by:
Hello, could someone explain the difference to me inbetween: *ptr++ and ++*ptr Thankx a lot.. Jason.
8
by: Fredrik Tolf | last post by:
Hi list! I'm relatively new to Python, and one thing I can't seem to get over is the lack of in-expression assignments, as present in many other languages. I'd really like to know how Python...
14
by: howa | last post by:
void reverse_string(char *str) { if (str == NULL) return; char tmp; size_t len = strlen(str); size_t mid = (int) len / 2; for (size_t i = 0; i < mid; i++) {
28
MMcCarthy
by: MMcCarthy | last post by:
Policies below superceded by FAQ Post Course Work Questions and Answers. ADMIN
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.