[NEWBIE] datatype byte and &

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hey

Below are some c# code, the c variable gets the value 122. I know that the
datatype byte can hold a numeric value up to 255. I don't understand how (a
& b) becomes 122...

byte a, b, c;
a = 255;
b = 122;
c = (byte)(a & b);

Maybe some of you could give me some hints about why it becomes 122?

cheers

jeff
 
Jeff said:
Below are some c# code, the c variable gets the value 122. I know that the
datatype byte can hold a numeric value up to 255. I don't understand how (a
& b) becomes 122...

byte a, b, c;
a = 255;
b = 122;
c = (byte)(a & b);

Maybe some of you could give me some hints about why it becomes 122?

It's doing a bitwise "and" - and the bitwise "and" of 122 and 255 is
122.

What were you expecting, and why?

Jon
 
What is a bitwise and? As a layman reading this he was probabally
expecting what i was some kind of error? I thought that would simply
literally add the two numbers together?

What exactly is a bitwise and?

Thanks Jon,

Gary.
 
Hey

Below are some c# code, the c variable gets the value 122. I know that the
datatype byte can hold a numeric value up to 255. I don't understand how (a
& b) becomes 122...

byte a, b, c;
a = 255;
b = 122;
c = (byte)(a & b);

Maybe some of you could give me some hints about why it becomes 122?

cheers

jeff
The '&' operator is the "bitwise and" operator. It works on the
binary representations of the two numbers:

255 = 11111111 binary
122 = 01111010 binary

Bitwise and looks like:

&| 0 1
--+-----
0| 0 0
1| 0 1

1 & 1 = 1, all other combinations are zero.

Hence

255 is 11111111
122 & 01111010 &
---- ---------
122 01111010

Because 255 is all ones, any number in the range 0 - 255 will give
itself when anded with 255.

rossum
 
What is a bitwise and? As a layman reading this he was probabally
expecting what i was some kind of error? I thought that would simply
literally add the two numbers together?

What exactly is a bitwise and?

It takes two numbers, and "ands" together each bit - so each bit of the
result is only 1 if the corresponding bit in both of the inputs is 1.

I would be very wary of guessing what operators do though - the only
situation I know of in which "&" is used for some sort of addition is
with strings in some languages (eg VB). Everywhere else it's AND,
either logical or bitwise.

Jon
 
thankyou very much i have an idea of what it is about now whereas
before i had no idea.

one last question on this from me. I'm struggling with my very limited
knowledge to think of when this might be useful, what sort of tasks is
this used for?

(i started learning VB before i decided c# was a better choice to start
with, i think that's why i got confused with the &, like you said Jon -
i had used it with string previously!)
 
Hi Jeff,

AND (&) is useful for reading certain bits.

If the lowest two bits in a byte indicates some value, reading those bits
when all you are given is a whole byte you will first need to filter out
the bits you don't need.

byte b = 174; // 10101110
byte filter = 3; // which is 11 in binary
int value = b & filter; // all arithmetic will end up as integer or higher

10101110 (174)
&
00000011 (3)
=
00000010 (2)

The lowest two bits of this number is equal to 2.

Another more commonly usage is reading enumerated values.
For instance, in a KeyUp/KeyDown event you will get a KeyEventHandler with
a Modifiers value, which is essentially a number. In the code below
Keys.Shift would be the filter.

protected override void OnKeyUp(KeyEventArgs e)
{
if((e.Modifiers & Keys.Shift) > 0)
// shift is already pressed when we get this key
}
 
Back
Top