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
    10,148
    1,791
    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.
     
  6. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    Thank you for this information, but I need a little clarification because I do not understand exactly how this operator works.

    If we have 0b00000001 and 0b00000001 we need to take the last four digits and make 0b00010001 or we can have 0b0001 and 0b0001 and we have to make 0b00010001. I am a little bit on the dark with the binary logic combinations. I know how to do the summing of two variables and other things but I do not know how to take the last 4 digits and make them in to 1 variable.
     
  7. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    Maybe this:
    FinalFormOfThePORTCOutput = (BinaryFormOfSecondDigit >> 4) & BinaryFormOfFirstDigit;

    The result should be:
    FinalFormOfThePORTCOutput = (0b00000001 >> 4) & 0b00000001;

    We shift the second digit with four bits and mask with the first digit.

    Meaning we get 0b00010001?
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    The shifting operators >> and << just shift. The do not rotate bits in a circular fashion like some assembly language instructions. The result of 1 >>4 is zero in the same way that for a byte operation 0x80 <<4 would be 0.
     
  9. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    So from this 0b00000001 >>4
    I get this: 0b00010000 ?

    It is difficult to test it with the compiler with no display and I do not want to connect a virtual teminal just to check.
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    No No No. Shifting is NOT the same as rotating! Bits shifted right past the least significant bit just disappear. Similarly bits shifted left past the most significant bit also disappear, by falling into the bit-bucket.

    You can tell what the compiler is doing by getting an assembly language listing or object file disassembly.

    In fact an optimizing compiler wouldn't even bother doing the shift operation. It would just use the literal constant zero.
     
  11. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    Thats bad, now I understand.
    So we get this:

    0b00000001 >>4

    result: 0b0001 ?

    If I am right, despite that I am sleeping at the moment since I came back from a hard shift at work.

    Is there anyway to combine 0b00010000 and 0b00000001 and get 0b00010001?
    Or combine 0b0001 and 0b0001 and get 0b00010001?
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    No No No 0b00000001 >> 4 result 0b00000000

    (0b0001 << 4) | (0b0001) evaluates to 0b00010001

    also
    0b00010000 | 0b00000001 evaluates to 0b00010001

    The vertical bar is the bitwise inclusive OR operation
     
  13. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    By OR you mean binary plus.
    So we have 0b00010000 + 0b00000001?
     
  14. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    I did it like this:
    It might work.

    const unsigned int8 TableForDigits[10] = {0b00000000, 0b00000001, 0b00000010, 0b00000011,
    0b00000100, 0b00000101, 0b00000110, 0b00000111, 0b00001000, 0b00001001};

    const unsigned int8 TableForSecondDigits[10] = {0b00000000, 0b00010000, 0b00100000, 0b00110000, 0b01000000,
    0b01010000, 0b01100000, 0b01110000, 0b10000000, 0b10010000};



    FirstDigit = Display%10;
    SecondDigit = Display/10;

    for(i=0; i<=10; i++)
    {
    if(FirstDigit == i)
    BinaryFormOfFirstDigit = TableForDigits;
    if(SecondDigit == i)
    BinaryFormOfSecondDigit = TableForSecondDigits;
    }
    FinalFormOfThePORTCOutput = BinaryFormOfSecondDigit | BinaryFormOfFirstDigit;

    output_c(FinalFormOfThePORTCOutput);
    delay_us(100);
    output_high(PIN_B5);

    It is for driving two 7 segment displays, without using a macro with two 4511 decoders. The second digit is in the second 4 bits and the first digit is in the first four bits. The idea was to use one table instead of two, to spare resources. With two tables the code is like this.
     
  15. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    No I do not mean binary plus. OR is a boolean operation that takes place on a bit by bit basis with no carries. The truth table looks like:

    0 | 0 = 0
    0 | 1 = 1
    1 | 0 = 1
    1 | 1 = 1
     
  16. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    That truth table is similar to binary summing.

    1+1=0 remainder 1
    0+0=0 remainer 0
    1+0=1 remainder 0
    0+1=1 remainer 0

    That is why I ask.
    I need to sum two binary numbers like this:

    0b00010000+ 0b00001000
     
  17. ArakelTheDragon

    New Member

    Nov 18, 2016
    14
    0
    The result should be 0b0001 1000
     
  18. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    That will work exactly as you wrote it, but what if it were:
    result = 0b00010000+ 0b00011000; // Note that bit 4 is set in both bytes

    Would you want to get 0b00101000, or 0b00011000? In the first case it's
    result = a+b
    and in the second it's
    result = a|b
     
  19. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    Just because two things are similar, does make them equivalent. We have a number of different operations because there are different behaviors we need to use.

    BTW it is semantically incorrect to use the word remainder in conjunction with a summing operation. The word remainder is associated with the division operation. The correct term in the context of summation is carry. In the case of subtraction it is borrow. To round out the four arithmetic operations we use the word carry in the context of multiplication. Using the correct terminology can be important for those trying to learn.
     
  20. KeepItSimpleStupid

    Well-Known Member

    Mar 4, 2014
    1,147
    204
    I changed your numbers. Zero isn't interesting.

    You take A and shift it left 4 positions and get 11110000
    Then a Binary OR of 0001

    00001111; A= 15 decimal
    11110000; A shifted 4 which is also 15 * 16 = 128 + 64+ 32+16 = 240
    00000001; B
    11110001; Binary OR of the shifted A with B; result = 241

    With the numbers you have, you can also multiply by a power of 2 (16) which shifts left and add B.
    16 shifts left 4.
     
Loading...