c code explaination

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi

I don't understand what does this do in the following code: (0##x & 01000)/64 , can someone explain it to me, thanks

Rich (BB code):
#define UTIL_BIN4(x)        (uchar)((0##x & 01000)/64 + (0##x & 0100)/16 + (0##x & 010)/4 + (0##x & 1))
 

WBahn

Joined Mar 31, 2012
29,978
The ## is the preprocessor's token pasting operator.

In this case, it appears to be taking whatever value is passed to the macro (presumable a number consisting of the digits 0-7 only) and is gluing a zero to the front of it to make it be interpretted as an octal value.

IIt looks like it is picking off bits 9,6,3,0 from the input value and packing them into bits 3,2,1,0, respectively, of the result.
 

WBahn

Joined Mar 31, 2012
29,978
My guess is that the purpose of this is to permit you to enter values in binary and get them packed into a nibble.

So if you called UTIL_BIN4(1001) the return value would be 9.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
The ## is the preprocessor's token pasting operator.

In this case, it appears to be taking whatever value is passed to the macro (presumable a number consisting of the digits 0-7 only) and is gluing a zero to the front of it to make it be interpretted as an octal value.
The main thing I want to know is ##, all it does in this case is to glue the 0 and x (whatever it may represent) together to form a 0x?
 

WBahn

Joined Mar 31, 2012
29,978
Yes, however I believe that the x has to be something that is known at compile time. So you could NOT use this within your code where x is a variable of some kind.
 
Top