tags:

views:

117

answers:

3
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] & 0xcf) | (saltLen & 0x30));
    salt[3] = (byte)((salt[3] & 0x3f) | (saltLen & 0xc0));
A: 
// 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 :)

Nippysaurus
Presumably he meant to ask how it does it, not what it does. Of course, why it does it is the more interesting question.
MZB
+2  A: 

Well the comment above explains what it's doing, but if you're looking for a breakdown of the operators:

  1. Perform a bitwise and on salt[i] and a hex number (the & operator).
  2. Perform a bitwise and on salt[i] and a second hex number.
  3. Perform a bitwise or on the result of steps 1 and 2 (the | operator).
  4. Cast the result of step 3 to a byte
  5. 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:

eldarerathis
+3  A: 

the question is about follow operators: & and |,and 0xfc

Joe White