The code is strange and does unnecessary things.
The operation of shift (operator <<
) moves bits to the left. As the position determines the number represented, as in the decimal, each bit shifted to the left multiplies by 2 (the base of the binary) the existing value (as well as shifting a digit to the left in the decimal multiplies by 10, the base of the decimal). In this case each pass through the loop will produce 1, 2, 4, 8, etc.
The result will be calculated with the variable Hex
doing an operation of and (operador &
). The and works similarly to multiplication, i.e., each bit that one operand is calculated with the corresponding Bite of the other operand. The result will be 1 whenever both are 1 and 0 when there is no such coincidence.
The final result being a non-zero value executes the if
, since, in C, this is considered true. If the result is 0, executes the else
.
Learn more here. The language is different but the working is the same.
The
if
works the same, no matter what has in it, you want to know how the operator of shift works? Be more specific.– Maniero
I want to know the context of the operation. what are you doing
– alexsander
Notice that add up
0
(or0 * qualquer coisa
) is the same as not adding up: you can remove theelse
of the code :-)– pmg
You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Something needs to be improved?
– Maniero