Bitwise And with one it will extract a bit pattern from the other operand. Value 10101 & 11111 = 10101 . If the result of this bitwise AND is 0, then we know that we know that the other operand is 0. The result is 0 when ANDing of one byte with 0xFF (ones) will indicate NULL bytes.
The code itself checks every byte of the char array in four-byte partitions. NOTE. This code is not portable; on another machine or compiler, unsigned int can be more than 4 bytes. It is probably best to use the uint32_t data type to provide 32-bit unsigned integers.
The first thing to note is that on a machine with small bytes, the bytes that make up the character array will be read in the unsigned data type in reverse order; that is, if the four bytes at the current address are the bitmap corresponding to abcd , then the unsigned variable will contain the bit pattern corresponding to dcba .
The second is that the constant of a hexadecimal number in C results in an int-size number with the specified bytes at the low end of the bitmap. The value 0xFF is actually 0x000000FF when compiling with 4-byte ints. 0xFF00 0x0000FF00 . And so on.
Thus, the program basically searches for the NULL character in four possible positions. If there is no NULL character in the current section, it moves to the next four-byte slot.
Take the char abcdef array for example. In C, string constants will always have null terminators at the end, so there is a 0x00 byte at the end of this line.
It will work as follows:
Read "abcd" in unsigned int x:
x: 0x64636261 [ASCII representations for "dcba"]
Check each byte for a null terminator:
0x64636261 & 0x000000FF 0x00000061 != 0, 0x64636261 & 0x0000FF00 0x00006200 != 0,
And check out the two other positions; There are no null terminators in this 4-byte section, so move on to the next section.
Read "ef" in unsigned int x:
x: 0xBF006665 [ASCII representations for "fe"]
Pay attention to the bytes 0xBF; this is minus the length of the string, so we read garbage from the runtime stack. It could be anything. On a machine that does not allow unrelated accesses, this will crash if the memory after the line is not aligned by 1 byte. If only one character remained in the string, we would read two additional bytes, so the alignment of the memory adjacent to the char array would have to be aligned by 2 bytes.
Check each byte for a null terminator:
0xBF006665 & 0x000000FF 0x00000065 != 0, 0xBF006665 & 0x0000FF00 0x00006600 != 0, 0xBF006665 & 0x00FF0000 0x00000000 == 0 !!!
So we return len + 2 ; len was 4 since we incremented it once by 4, so we return 6, which is really the length of the string.