c - How do I convert a char array [example 42] to the int equivilant [int 42] using bit manipulation? -
say have string 42\0 0b0011010000110001000000000 [or array without null terminator 42 0b00110100001100010] , want convert 42 0b00101010 bit manipulation. how go this?
in nutshell, without checks number digit, negative, or out of range, can this:
int myatoi( const char * s ) { int result = 0; while( *s ) { result <<= 1; result += (result << 2); result += (*s++ & 0x0f); } return result; }
caveat: use of addition doesn't strictly meet requirement achieved bit operations.
Comments
Post a Comment