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
    Messages:
    28
    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

    recca02 Senior Member

    Joined:
    Apr 2, 2007
    Messages:
    1,211
    Location:
    India-22.35° N 82.68° E.
    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_

    Dave_ Thread Starter Member

    Joined:
    Mar 22, 2007
    Messages:
    28
    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

    Papabravo AAC Fanatic!

    Joined:
    Feb 24, 2006
    Messages:
    7,697
    Location:
    Michigan, USA (GMT-5)
    If you know the length of the strings you can combine them with shifting and boolean operations.
    Code:
    C = (A<<4) | B ;
    
    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_

    Dave_ Thread Starter Member

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

Share This Page