473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Am I an idiot?

That's a rethorical question. But shouldn't this statement store the value
of three in the variable a:

#include <iostream>

int main()
{
int a = 1 & 2;
if(a & 1)
{
std::cout << "One " << std::endl;
}
if(a & 2)
{
std::cout << "Two " << std::endl;
}
return 0;
}

I'm trying to use bit-masking to pass around flags with an integer, and then
check them with if( integer & flagX )

What is going on?

Thanks a lot for your time,
Kevin Grigorenko
Jul 22 '05 #1
11 1622
Kevin Grigorenko wrote:

That's a rethorical question. But shouldn't this statement store the value
of three in the variable a:
No. It should store the value 0.

#include <iostream>

int main()
{
int a = 1 & 2;


You want

int a = 1 | 2;
Use | to set bits, use & to clear bits or to check if
a bit is set.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Kevin Grigorenko wrote in news:br**********@f04n12.cac.psu.edu:
That's a rethorical question. But shouldn't this statement store the
value of three in the variable a:

#include <iostream>

int main()
{
int a = 1 & 2;
int a = 1 | 2;
if(a & 1)
{
std::cout << "One " << std::endl;
}
if(a & 2)
{
std::cout << "Two " << std::endl;
}
return 0;
}

I'm trying to use bit-masking to pass around flags with an integer,
and then check them with if( integer & flagX )

What is going on?


& (aka bitwise and aka bitand) both things must be true (or set in a
bitwise sense).

| (aka bitwise or aka bitor ) one (or both) must be true (or ...)

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...
Kevin Grigorenko wrote:

That's a rethorical question. But shouldn't this statement store the value of three in the variable a:
No. It should store the value 0.

#include <iostream>

int main()
{
int a = 1 & 2;


You want

int a = 1 | 2;


Ahhh, so I am an idiot. I knew that. Haha, wow, what is wrong with me
today.


Use | to set bits, use & to clear bits or to check if
a bit is set.
--
Karl Heinz Buchegger
kb******@gascad.at


Appreciate it,
Kevin Grigorenko
Jul 22 '05 #4
Kevin Grigorenko wrote:
Shouldn't this statement store the value
of three in the variable a:

#include <iostream>

int main()
{
int a = 1 & 2;
You probably meant to write

int a = 1 | 2;
if(a & 1)
{
std::cout << "One " << std::endl;
}
if(a & 2)
{
std::cout << "Two " << std::endl;
}
return 0;
}

I'm trying to use bit-masking to pass around flags with an integer
and then check them with if( integer & flagX )

What is going on?


Nothing. You're just an idiot. ;-)

Jul 22 '05 #5
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:3F**************@jpl.nasa.gov...
Kevin Grigorenko wrote:
Shouldn't this statement store the value
of three in the variable a:

#include <iostream>

int main()
{
int a = 1 & 2;


You probably meant to write

int a = 1 | 2;
if(a & 1)
{
std::cout << "One " << std::endl;
}
if(a & 2)
{
std::cout << "Two " << std::endl;
}
return 0;
}

I'm trying to use bit-masking to pass around flags with an integer
and then check them with if( integer & flagX )

What is going on?


Nothing. You're just an idiot. ;-)


I agree :). Thanks for the quick help to all who responded. It is now
confirmed that I am an idiot.

Kevin Grigorenko
Jul 22 '05 #6

"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:br**********@f04n12.cac.psu.edu...

I'm trying to use bit-masking to pass around flags with an integer, and then check them with if( integer & flagX )


But the bits for 1 are 01, and for 2 are 10. When you AND them together,
there aren't any ones. Did you mean OR?
Jul 22 '05 #7
"jeffc" <no****@nowhere.com> wrote:
But the bits for 1 are 01, and for 2 are 10. When you AND them together,
there aren't any ones. Did you mean OR?


The bit patterns for 1 and 2 are not absolutely guaranteed to be "000...01"
and "000...10" - the "two's complement" format represents them this way,
but theoretically they could be anything they like (I'm not sure if any
computers actually represent them a different way, but I've heard of "one's
complement" where negative numbers are stored differently).

0x1 and 0x2 ought to be pretty safe, though ...

David F

PS. Pretty sure about this, but correct me if I'm wrong - it's kind of
unintuitive for the expression (0x0F == 15) to not always be true, since 0F
_is_ 15 in hexadecimal ...
Jul 22 '05 #8

"David Fisher" <no****@nospam.nospam.nospam> wrote in message
news:do******************@nasal.pacific.net.au...
"jeffc" <no****@nowhere.com> wrote:
But the bits for 1 are 01, and for 2 are 10. When you AND them together, there aren't any ones. Did you mean OR?
The bit patterns for 1 and 2 are not absolutely guaranteed to be

"000...01" and "000...10" - the "two's complement" format represents them this way,

..
Actually, it has nothing to do with two's complement. The C standard
REQUIRES
the positive numbers to be simply straight forward binary. The rules (for
C++ and
C89) say that unsigned values are pretty much straight forward binary and
that the
positive signed variables have the same representation as their unsigned
counterparts.
The C99 standard goes even further and says that there is only three legal
integer
encodings: 2's complement, 1's complement, and signed-magnitude. All of
which
obey the earlier constraint.

-> 0x1 and 0x2 ought to be pretty safe, though ...

This is a pretty bizarre statement give your earlier statement. 1 and 0x1
are
exactly the same value as var as the language is concerned (the hex
specified
numbers only differ in that they will have type unsigned int if int can't
represent them).

Jul 22 '05 #9
David Fisher wrote:
...
But the bits for 1 are 01, and for 2 are 10. When you AND them together,
there aren't any ones. Did you mean OR?


The bit patterns for 1 and 2 are not absolutely guaranteed to be "000...01"
and "000...10" - the "two's complement" format represents them this way,
but theoretically they could be anything they like (I'm not sure if any
computers actually represent them a different way, but I've heard of "one's
complement" where negative numbers are stored differently).
...


Positive integral values are represented in the same way in all binary
formats supported by C/C++ specifications. In other words, 1 is
guaranteed to be '0..001' and 2 is guaranteed to be '0..010'.

Note, that "bits" that are mentioned in the language specification are
language-level abstractions. The actual underlying hardware might even
be non-binary (ternary, for example) and have nothing that can be
regarded as physical "bits".

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #10
"Andrey Tarasevich" <an**************@hotmail.com> wrote:
Positive integral values are represented in the same way in all binary
formats supported by C/C++ specifications. In other words, 1 is
guaranteed to be '0..001' and 2 is guaranteed to be '0..010'.

Note, that "bits" that are mentioned in the language specification are
language-level abstractions. The actual underlying hardware might even
be non-binary (ternary, for example) and have nothing that can be
regarded as physical "bits".


OK, my mistake ...

Thanks for the info :-)

David F
Jul 22 '05 #11
It looks like you used the wrong logical operator to initialize the
variable "a".

If you logically AND the value 1 with the value 2, the result is zero.

You probably want to OR the two values together.

Replace:
int a = 1 & 2;

With:
int a = 1 | 2;

Kevin Grigorenko wrote:
That's a rethorical question. But shouldn't this statement store the value
of three in the variable a:

#include <iostream>

int main()
{
int a = 1 & 2;
if(a & 1)
{
std::cout << "One " << std::endl;
}
if(a & 2)
{
std::cout << "Two " << std::endl;
}
return 0;
}

I'm trying to use bit-masking to pass around flags with an integer, and then
check them with if( integer & flagX )

What is going on?

Thanks a lot for your time,
Kevin Grigorenko


Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: C. Armour | last post by:
Help me, I'm suffering! Situation: I have a load() function which loads a bunch of TextFields with values. There are TextListeners registered for each of these TextFields. Thus, changing the...
3
by: olsongt | last post by:
I'm trying to be a good boy and use the logging module but it's behaving rather counter-intuitively. I've posted a simplified test script below. To summarize, I've set the basic config to only...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
15
by: Daniel Billingsley | last post by:
Speaking of trying to read deeply nested if-else blocks... I often find it's not always easy to tell one indent level from another (granted I keep my tab settings low so I'm not halfway across...
4
by: Bill Dodd | last post by:
Is there really no way to have different asp.net applications share session variables? The problem I'm running into is that I have numerous asp.net (and asp) applications that were written as...
12
by: clintonG | last post by:
I can't tell you how frustrated I get when going to a web developer's website and observing he or she is an idiot that has not grasped the most fundamental element of usability: page title naming...
23
by: novice | last post by:
I dint find a proper group to post this, so i'm asking this question. If you think this question is irrelevent then dont answer, but i expect good replies from experts. I wondor, how these guys...
2
by: Fishmonger | last post by:
Hi Guys, I'm looking for a good C++ compiler that will work well with The Complete Idiot's Guide to C++.I've tried DEV and Microsoft Visual C++ express.They all come up with "iostream "error.If...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.