Connecting Tech Pros Worldwide Help | Site Map

Am I an idiot?

Kevin Grigorenko
Guest
 
Posts: n/a
#1: Jul 22 '05
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


Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Am I an idiot?


Kevin Grigorenko wrote:[color=blue]
>
> That's a rethorical question. But shouldn't this statement store the value
> of three in the variable a:[/color]

No. It should store the value 0.
[color=blue]
>
> #include <iostream>
>
> int main()
> {
> int a = 1 & 2;[/color]

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
kbuchegg@gascad.at
Rob Williscroft
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Am I an idiot?


Kevin Grigorenko wrote in news:br7km3$ds0$1@f04n12.cac.psu.edu:
[color=blue]
> 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;[/color]

int a = 1 | 2;
[color=blue]
> 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?
>[/color]

& (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/
Kevin Grigorenko
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Am I an idiot?


"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:3FD75759.F3CE3DA2@gascad.at...[color=blue]
> Kevin Grigorenko wrote:[color=green]
> >
> > That's a rethorical question. But shouldn't this statement store the[/color][/color]
value[color=blue][color=green]
> > of three in the variable a:[/color]
>
> No. It should store the value 0.
>[color=green]
> >
> > #include <iostream>
> >
> > int main()
> > {
> > int a = 1 & 2;[/color]
>
> You want
>
> int a = 1 | 2;[/color]

Ahhh, so I am an idiot. I knew that. Haha, wow, what is wrong with me
today.
[color=blue]
>
>
> Use | to set bits, use & to clear bits or to check if
> a bit is set.
>
>
> --
> Karl Heinz Buchegger
> kbuchegg@gascad.at[/color]

Appreciate it,
Kevin Grigorenko


E. Robert Tisdale
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Am I an idiot?


Kevin Grigorenko wrote:
[color=blue]
> Shouldn't this statement store the value
> of three in the variable a:
>
> #include <iostream>
>
> int main()
> {
> int a = 1 & 2;[/color]

You probably meant to write

int a = 1 | 2;
[color=blue]
> 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?[/color]

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

Kevin Grigorenko
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Am I an idiot?


"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> wrote in message
news:3FD757C2.9010508@jpl.nasa.gov...[color=blue]
> Kevin Grigorenko wrote:
>[color=green]
> > Shouldn't this statement store the value
> > of three in the variable a:
> >
> > #include <iostream>
> >
> > int main()
> > {
> > int a = 1 & 2;[/color]
>
> You probably meant to write
>
> int a = 1 | 2;
>[color=green]
> > 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?[/color]
>
> Nothing. You're just an idiot. ;-)[/color]

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

Kevin Grigorenko


jeffc
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Am I an idiot?



"Kevin Grigorenko" <kzg110@psu.edu> wrote in message
news:br7km3$ds0$1@f04n12.cac.psu.edu...[color=blue]
>
> I'm trying to use bit-masking to pass around flags with an integer, and[/color]
then[color=blue]
> check them with if( integer & flagX )[/color]

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?


David Fisher
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Am I an idiot?


"jeffc" <nobody@nowhere.com> wrote:
[color=blue]
> 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?[/color]

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


Ron Natalie
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Am I an idiot?



"David Fisher" <nospam@nospam.nospam.nospam> wrote in message
news:doOBb.5286$xm.150993@nasal.pacific.net.au...[color=blue]
> "jeffc" <nobody@nowhere.com> wrote:
>[color=green]
> > But the bits for 1 are 01, and for 2 are 10. When you AND them[/color][/color]
together,[color=blue][color=green]
> > there aren't any ones. Did you mean OR?[/color]
>
> The bit patterns for 1 and 2 are not absolutely guaranteed to be[/color]
"000...01"[color=blue]
> and "000...10" - the "two's complement" format represents them this way,[/color]
..
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).



Andrey Tarasevich
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Am I an idiot?


David Fisher wrote:[color=blue]
> ...[color=green]
>> 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?[/color]
>
> 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).
> ...[/color]

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

David Fisher
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Am I an idiot?


"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote:
[color=blue]
> 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".[/color]

OK, my mistake ...

Thanks for the info :-)

David F


Tron Thomas
Guest
 
Posts: n/a
#12: Jul 22 '05

re: Am I an idiot?


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:
[color=blue]
> 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
>
>[/color]

Closed Thread