473,769 Members | 2,166 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
11 1652
"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
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
1450
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
12241
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
1551
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
1700
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
1859
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
2031
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
1551
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
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10206
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
10035
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...
1
9984
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
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
7403
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
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.