Menu

Preprocessor Directives & Syntax Used

Steve_M
2008-12-05
2012-09-26
  • Steve_M

    Steve_M - 2008-12-05

    Hi to all. A question on C preprocessor directives. I came across the following #define in a program header file associated with the ELF file format.

    define PF_X (1<<0)

    When PF_X is encountered in the program text, what is the value that is actually inserted in its place.........in other words.......what does (1<<0) mean???

    Here are some more examples of the syntax......One of the definitions below includes & which has an obvious meaning......but << and >> are not so obvious.

    //Manipulation of st_info values

    define ELF32_ST_BIND(i) ((i)>>4)

    define ELF32_ST_TYPE(i) ((i)&0xf)

    define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))

     
    • cpns

      cpns - 2008-12-05

      It means exactly what it says, 1 shifted by zero bits. Whether the compiler actually generates code to perform a zero bit shift is probably down to thw compiler and the processor instruction set. I would imagine that most compilers are smart enough to simply replace that with a literal constant 1, since it is not hard for teh pre-processor to determine that it is a constant expression and so evaluate it at runtime.

      The code may seem pointless, but such code is often written thus merely for consistency with similar and adjacently defined bit-mask macros that may not be bit zero, and also to support code maintenance - so for example the bit mask can be easily changes simply by changing the right-hand bit position operand.

      Without knowing the context, for thw following:

      define ELF32_ST_BIND(i) ((i)>>4)

      define ELF32_ST_TYPE(i) ((i)&0xf)

      define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))

      I would say that an ELF32 INFO byte comprises of a BIND field and a TYPE field, where each field is a four bit value, and BIND is packed into the upper nibble, while TYPE is in the lower nibble, so for example, an INFO byte 0x5a has a BIND = 5, and TYPE= 10 (0xa), the macros provide methods of extracting the fields and also for creating an INFO byte from the separate field values. So for example:

      ELF32_ST_INFO(10, 5 ) == 0xa5

      Clifford

       
    • cpns

      cpns - 2008-12-05

      For completeness, more examples:

      ELF32_ST_BIND( 0xab ) == 0xa (10 decimal)
      ELF32_ST_TYPE( 0xab ) == 0xb (11 decimal)

       
    • Steve_M

      Steve_M - 2008-12-05

      Thank you Clifford.

      A very well explained answer to my question.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.