How to concatenate binary in C

Thread Starter

Dave_

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

recca02

Joined Apr 2, 2007
1,214
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.
 

Thread Starter

Dave_

Joined Mar 22, 2007
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.
 

Papabravo

Joined Feb 24, 2006
14,410
If you know the length of the strings you can combine them with shifting and boolean operations.
Rich (BB 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.
 

ArakelTheDragon

Joined Nov 18, 2016
1,350
If you know the length of the strings you can combine them with shifting and boolean operations.
Rich (BB 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.
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.
 

ArakelTheDragon

Joined Nov 18, 2016
1,350
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?
 

Papabravo

Joined Feb 24, 2006
14,410
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.
 

ArakelTheDragon

Joined Nov 18, 2016
1,350
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.
 

Papabravo

Joined Feb 24, 2006
14,410
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.
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.
 

ArakelTheDragon

Joined Nov 18, 2016
1,350
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?
 

Papabravo

Joined Feb 24, 2006
14,410
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
 

ArakelTheDragon

Joined Nov 18, 2016
1,350
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.
 

ArakelTheDragon

Joined Nov 18, 2016
1,350
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
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
 

John P

Joined Oct 14, 2008
1,836
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
 

Papabravo

Joined Feb 24, 2006
14,410
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
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.
 
A = 1111
B = 0001

C = A & B

C now equals 11110001
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.
 
Top