Currently studying bitwise arithmetic. It’s really easy, because I have some CS background. But I just don’t understand one moment with this operator.
For example:
variable3 = variableOne & 3;
or
variable3 &= 3;
Actually this doesn’t matter.
I don’t understand how the process of setting the bits to 0 is going on. And how you can process it on the paper?
Let’s say
5&3, four-bit width:You just
&the bits in the same column. And since the&operator only returns1when both arguments are1, the higher bits from5not present in3are masked out.As for your example from the comments:
And now:
…and just to validate:
The schema is simple, you just
&each column:Where
zisx&y.Aha, judging by your comments in the neighbouring answer the problem is elsewhere. Numeric variables do not contain “hexadecimal values” in them. Numeric variables contain a bit pattern representing a number. “A number” is never binary, decimal or hexadecimal. When you say “three”, there’s no number system in play, three is a three no matter what.
When you say something like
var x = 0x76in the source code, the machine reads the hexadecimal representation of the number, creates a bit pattern representing this number and stores it in the memory corresponding to the variable. And when you then say something likex &= 3, the machine creates a bit pattern representing number three, combines that with the bit pattern stored in the variable and stores the result in the variable.