473,756 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ 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 1648
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.p su.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******@gasca d.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.p su.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******** **********@nasa l.pacific.net.a u...
"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

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

Similar topics

2
2313
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 text in any of them fires off a TextEvent, even if done programmatically (by means of setText()). The problem: I need all the various TextEvents fired off during the load operation (as a result of using TextField.setText())
3
1449
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 log root level messages with >= ERROR level. I've created another named logger that logs on info level. I've set it up so it just shows the message text without "INFO:: logger:" boilerplate. The way I see things, when I call otherLogger.info,...
383
12207
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 poor man's DBMS, a broken email server and various other /application/ servers to try and crack the Internet and IS markets. In the case where they didn't spend their own money to get companies to
15
1548
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 the page by the 3rd level), and I find myself doing things like this to help me keep it straight: class MyClass { public void SomeMethod()
4
1699
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 standalone apps and are now one by one being added to an umbrella project. For instance OpenOrderReport is a asp project with numerous references to third part controls, etc. It's existed a http://where-ever/Openorderreport for months and is...
12
1857
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 conventions. 1.) You know you are at an idiot's website when there is no page title. Listen up idiot. Give every page a name using the HTML <title> element. 2.) When naming your page do not put the name of your website or your company 'after'...
23
2027
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 who answer to complicated questions have mastered C. I'm a beginner in C, and wondor how to master C. Should i study theory, or start writing programs? If i should start writing programs, What programs should i start with?
2
1549
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 you can give me a link I will be happy . Thanks Errol
0
9152
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9571
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4996
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3185
muto222
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.