0

as i want to concatenate bits together to be 0b0000 1110 but the output in the intermediate file is 0b0000B3B2B1B0

#include<stdio.h>



#define conc(bit3,bit2,bit1,bit0) ob##0000##bit3##bit2##bit1##bit0
#ifdef conc
#undef conc
#define B0  0
#define B1  1
#define B2  1
#define B3  1
#endif

#define conc(bit3,bit2,bit1,bit0) ob##0000##bit3##bit2##bit1##bit0
int main()
{
    int z=conc(B3,B2,B1,B0);
    printf(z);
}
sara hamdy
  • 402
  • 2
  • 12
  • First, fix the printing: `printf(z);` --> `printf("%8.8X\n",z);` If you had compiled with `-Wall`, your `printf` would have been flagged by the compiler. You're using `ob` and _not_ `0b` as the prefix, so that's another issue. – Craig Estey May 04 '21 at 23:25
  • 1
    [You need an extra layer of preprocessor abuse](https://stackoverflow.com/a/5256500/3386109). – user3386109 May 04 '21 at 23:37
  • ok, but my question is about preprocessor phase only not compilation phase @CraigEstey – sara hamdy May 04 '21 at 23:38
  • 1
    Follow the link from user3386109 or _don't_ abuse things and do: `#define S(_shf,_bit) (_bit << _shf) #define conc(bit3,bit2,bit1,bit0) S(3,bit3) | S(2,bit2) | S(1,bit1) | S(0,bit0)` But, honestly, although possible, what you're trying to do is "too cute". If I found it via a code review [in a commerical setting], I'd fix/remove it. Or: `static inline unsigned conc(unsigned b3,unsigned b2,unsigned b1,unsigned b0) { return (b3 << 3) | (b2 << 2) | (b1 << 1) | (b0 << 0); }` – Craig Estey May 04 '21 at 23:46
  • 1
    Echoing what others have suggested: if you have to ask for help to write it, others will ask for help to *read* it. Aim for clarity rather than clever. – Steve Friedl May 05 '21 at 01:26
  • Irrelevant to the problem, but wrong: The first word of your replacement text is `ob` (lower case 'Oh'), but I'm sure you want `0b` (zero). – the busybee May 05 '21 at 06:44
  • thanks it works #include #define B0 0 #define B1 1 #define B2 1 #define B3 1 #define conc(bit3,bit2,bit1,bit0) conc_help(bit3,bit2,bit1,bit0) #define conc_help(bit3,bit2,bit1,bit0) ob##0000##bit3##bit2##bit1##bit0 int main() { int z=conc(B3,B2,B1,B0); // instead of (1,1,1,0) directly printf("%d",z); – sara hamdy May 05 '21 at 14:48

0 Answers0