# The IF in recover

do not understand why the "if" statement offered by zamyla in the recover problem works. I can see the first three conditions but the last one is confusing.

``````if (array[0] == 0xff &&
array[1] == 0xd8 &&
array[2] == 0xff &&
(array[3] & 0xf0) == 0xe0)
``````

The `if` statement is to check if the file is a JPEG. All JPEGs start with `ffd8ff`. The next byte must be `0xe0`, `0xe1`, `0xe2`, `0xe3`, `0xe4`, `0xe5`, `0xe6`, `0xe7`, `0xe8`, `0xe9`, `0xea`, `0xeb`, `0xec`, `0xed`, `0xee`, or `0xef`, which means the next 4 bits must be `1110`. If you `&` (`and`) something, you compare each of the bits and returns a `1` only if both bits are `1`. so here it is:

``````  1110 xxxx (0xe~)
& 1111 0000 (0xf0)
1110 0000 (0xe0)
``````

This is because `0 & <0 or 1>` is always `0`. So no matter what `x` is, the result of `&`ing it with `0` is `0`. If the number doesn't start with `0xe`, this is the result:

``````  1010 1010 (0xaa)
& 1111 0000 (0xf0)
1010 0000 (0xa0)
``````

The last comparison is a two part sequence. The first part is what is called a mask and the second part is the comparison.

``````    (array[3] & 0xf0) == 0xe0
``````

Remember that this check of the 4th byte of the signature wants to make sure that the first 4 bits are 1110. Also, the second 4 bytes can be anything. So, to make this check easier, a mask is employed. The `&` operator is used to do a bit by bit AND between array[3] and 0xF0. Since `F` is 1111 in binary, it will perform an AND with the first 4 bits. Anything that is ANDed with 1 will return that value, so if the first 4 bits are e, or 1110, it will result in e.

Next, the last 4 bytes are 0000, and anything ANDed with 0 is 0. This means that the result of the AND will be 0.

Combine all that and it means that it will pass the first 4 bits through while passing only 0 for the last 4 bits. The result can be compared to 0xE0 to verify that the 4th byte is between 0xe0 and 0xEF.