How to concatenate binary in C

Discussion in 'Programmer's Corner' started by Dave_, May 15, 2007.

  1. Dave_

    Thread Starter Member

    Mar 22, 2007
    28
    0
    Hi all

    My wish is to concatenate two binary numbers into one variable size vector, such as below:

    A = 1111
    B = 0000

    C = A & B

    C now equals 11110000

    I found some solutions for C++, but none for C. Is it even possible?

    Thanks

    Dave
     
  2. recca02

    Senior Member

    Apr 2, 2007
    1,211
    0
    well i dont know much abt c,
    is concatenating strings different than concatenating binary numbers,
    otherwise i think even a noob like me can do it myself.
     
  3. Dave_

    Thread Starter Member

    Mar 22, 2007
    28
    0
    I was hoping there was a simple command to do it like '&' as used in VHDL. Perhaps I will have to do it the string way.
     
  4. Papabravo

    Expert

    Feb 24, 2006
    9,409
    1,542
    If you know the length of the strings you can combine them with shifting and boolean operations.
    Code ( (Unknown Language)):
    1.  
    2. C = (A<<4) | B ;
    3.  
    With strings you need to use library functions. There are no string concatenation operators in C. Literal strings can be concatenated explicitly as initializers or printf arguments.
     
  5. Dave_

    Thread Starter Member

    Mar 22, 2007
    28
    0
    Oh damn. Ok, cheers for that Papabravo.
     
Loading...