Am I an idiot?

K

Kevin Grigorenko

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
 
K

Karl Heinz Buchegger

Kevin said:
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.
 
E

E. Robert Tisdale

Kevin said:
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. ;-)
 
R

Rob Williscroft

Kevin Grigorenko wrote in
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.
 
K

Kevin Grigorenko

Karl Heinz Buchegger said:
No. It should store the value 0.


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.

Appreciate it,
Kevin Grigorenko
 
K

Kevin Grigorenko

E. Robert Tisdale said:
You probably meant to write

int a = 1 | 2;


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
 
J

jeffc

Kevin Grigorenko said:
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?
 
D

David Fisher

jeffc said:
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 ...
 
R

Ron Natalie

David Fisher said:
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).
 
A

Andrey Tarasevich

David said:
...

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".
 
D

David Fisher

Andrey Tarasevich said:
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
 
T

Tron Thomas

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;
 

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,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top