Strlen of MAX 16 chars string using bitwise operators

Posted by fabrizioM on Stack Overflow See other posts from Stack Overflow or by fabrizioM
Published on 2010-04-19T16:43:37Z Indexed on 2010/04/19 17:13 UTC
Read the original article Hit count: 391

Filed under:
|
|
|
|

The challenge is to find the fastest way to determine in C/C++ the length of a c-string using bitwise operations in C.

char thestring[16];

The c-string has a max size of 16 chars and is inside a buffer If the string is equal to 16 chars doesn't have the null byte at the end.

I am sure can be done but didn't got it right yet.

I am working on this at the moment, but assuming the string is memcpied on a zero-filled buffer.

len =   buff[0] != 0x0 +
            buff[1] != 0x0 +
            buff[2] != 0x0 +
            buff[3] != 0x0 +
            buff[4] != 0x0 +
            buff[5] != 0x0 +
            buff[6] != 0x0 +
            buff[7] != 0x0 +
            buff[8] != 0x0 +
            buff[9] != 0x0 +
            buff[10] != 0x0 +
            buff[11] != 0x0 +
            buff[12] != 0x0 +
            buff[13] != 0x0 +
            buff[14] != 0x0 +
            buff[15] != 0x0;

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++