C# strange code
Anyone know what follow code does?
the question is about follow operators: & and |,and 0xfc
salt[0] = (byte)((salt[0] & 0xfc) | (saltLen & 0x03));
salt[1] = (byte)((salt[1] & 0xf3) | (saltLen & 0x0c));
salt[2] = (byte)((salt[2开发者_如何学JAVA] & 0xcf) | (saltLen & 0x30));
salt[3] = (byte)((salt[3] & 0x3f) | (saltLen & 0xc0));
the question is about follow operators: & and |,and 0xfc
&
is the bitwise and operator. See http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx.|
is the bitwise or operator. See http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx.0xfc
isn't an operator, it's an integer constant (i.e., a number). See http://msdn.microsoft.com/en-us/library/aa664674(VS.71).aspx and http://en.wikipedia.org/wiki/Hexadecimal.
Well the comment above explains what it's doing, but if you're looking for a breakdown of the operators:
- Perform a bitwise
and
onsalt[i]
and a hex number (the&
operator). - Perform a bitwise
and
onsalt[i]
and a second hex number. - Perform a bitwise
or
on the result of steps 1 and 2 (the|
operator). - Cast the result of step 3 to a
byte
- Store the result in
salt[i]
The result is what is noted in the comment block. The numbers of the format 0xc0
and whatnot are in hexadecimal, which is base 16. I.e. c0
in hex is equivalent to 16*12 + 16*0 = 192
in decimal. In hex, since you run out of digits at 9, you begin using letters. Thus, a=10, b=11, c=12, d=13, e=14, f=15, and f becomes the highest "digit" since you would move over by one place when you get to 16 (as 16 is the base).
See also:
- Bitwise operation
- Hexadecimal
// Split salt length (always one byte) into four two-bit pieces and
// store these pieces in the first four bytes of the salt array.
This is a cocky answer, but my intention is to indicate that it is already answered, so please let me know if you need more detail :)
精彩评论