How to concatenate binary in C

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

  1. Dave_

    Dave_ Thread Starter Member

    Joined:
    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
     
    #1
  2. recca02

    recca02 Senior Member

    Joined:
    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.
     
    #2
  3. Dave_

    Dave_ Thread Starter Member

    Joined:
    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.
     
    #3
  4. Papabravo

    Papabravo AAC Fanatic!

    Joined:
    Feb 24, 2006
    7,900
    1,012
    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.
     
    #4
  5. Dave_

    Dave_ Thread Starter Member

    Joined:
    Mar 22, 2007
    28
    0
    Oh damn. Ok, cheers for that Papabravo.
     
    #5
Loading...