c code explaination

Discussion in 'Programmer's Corner' started by bug13, May 12, 2013.

  1. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    Hi

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

    Code ( (Unknown Language)):
    1. #define UTIL_BIN4(x)        (uchar)((0##x & 01000)/64 + (0##x & 0100)/16 + (0##x & 010)/4 + (0##x & 1))
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    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.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    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.
     
  4. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    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?
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    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.
     
  6. bug13

    Thread Starter Well-Known Member

    Feb 13, 2012
    1,208
    38
    My problem solved, thanks a lot :)
     
Loading...