How to concatenate binary in C

ArakelTheDragon

Joined Nov 18, 2016
1,362
For the first comment:
It is not possible that we have two "1s" because I need this to drive two 7 segment displays. The first four bits start the first digit and they can be from "0" to "9" and the same goes for the second 4 bits. The idea is this:


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

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

output_c(FinalFormOfThePORTCOutput);

I hope that from "SecondDigit/10" I get a result without a floating point, but I am not certain because my compiler gives int "8 bits" not "4 bits" or "1 bit."
Meaning "25/10 = 2", not "25/10 = 2.5"

That would give us a varibale with the first four bits filled as: 0b1000 0000
And the second variable would be 0b0000 1000
When I sum both I should get: 0b 1000 1000
Which is equal to 88 or 8 at the first dysplay and 8 at the second display.The first four bits never mix with second, they just need to be in one variable.
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
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.
I am sorry for that I did not knew! I will try to remember it but at the moment it is not likely. Sorry!
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
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.
This can work!

So I get the second digit 0b0000 1000 which is recorded in the variable "SecondDigit" and shift it:
SecondDigit >>4;
FinalOutputOfPORTC = SecondDigit & FirstDigit;

The result should be:
FinalOutputOfPORTC = 0b1000 0000 & 0b 0000 1000 =0b 1000 1000?
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
For the first comment:
It is not possible that we have two "1s" because I need this to drive two 7 segment displays. The first four bits start the first digit and they can be from "0" to "9" and the same goes for the second 4 bits. The idea is this:


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

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

output_c(FinalFormOfThePORTCOutput);

I hope that from "SecondDigit/10" I get a result without a floating point, but I am not certain because my compiler gives int "8 bits" not "4 bits" or "1 bit."
Meaning "25/10 = 2", not "25/10 = 2.5"

That would give us a varibale with the first four bits filled as: 0b1000 0000
And the second variable would be 0b0000 1000
When I sum both I should get: 0b 1000 1000
Which is equal to 88 or 8 at the first dysplay and 8 at the second display.The first four bits never mix with second, they just need to be in one variable.
Normally for summing I know that 1+1 =0 and we have a carry of 1?

Meaning 0b00101000 from summing.
And 0b00011000 is logical OR because we have binary OR logic, if one of the two bits is "1" we get "1" on the output? If both bits are "1" we get 1 on the output?

This is logcal AND -"&" or this is logical AND "&&" ? Meaning if both bits are "1" we get 1 on the output and if both are "0" we get "0" at the output? For anything else it is "0".

And the summing if I use "A+B", I will get 0+0=0, 0+1=1, 1+1=0 and a carry of 1?

I think all three options will work for what I need?
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
I think it should be like this?

Code:
const unsigned int8 TableForDigits[70] = {
     
      0b00000000, //Output for "0" for the first display from "FirstDigit = Display / 10"
      0b00000001, //1
      0b00000010, //2
      0b00000011, //3
      0b00000100, //4
      0b00000101, //5
      0b00000110, //6
      0b00000111, //7
      0b00001000, //8
      0b00001001, //9



FirstDigit = Display%10;      
       SecondDigit = Display/10;  
     
        for(i=0; i<10; i++)
        {
            if (FirstDigit == i)
                BinaryFormOfFirstDigit = TableForDigits[i];
            if(SecondDigit == i)
                BinaryFormOfSecondDigit = TableForDigits[i];
        }
        BinaryFormOfSecondDigit >>4;
        FinalFormOfThePORTCOutput = BinaryFormOfSecondDigit | BinaryFormOfFirstDigit;
      output_c(FinalFormOfThePORTCOutput);
 

Papabravo

Joined Feb 24, 2006
21,254
There is a subtle difference between & and &&. The single ampersand, &, is a bitwise operator. The double ampersand, &&, is used to combine realtional expressions into a single Boolean result.
Example:
a = b & (1<<2) ;​
a will be equal to 0 or 4 depending on the original value of bit 2 of b.

But,
a = (b > 0) || (c < 9)​
will be equal to 'true' if b > 0 OR c < 9 or if both of those expressions are 'true'. It will be false otherwise. That will happen according to DeMorgan's Law when b <= 0 AND c >= 9.

Got it?
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
I understand the binary logic that both conditions must be true for "AND" and only one condition must be true for "OR". What I need is what is every operator for and what is the difference between Bitwise and Binary operators?

This is Bitwise AND- & ?
This is AND- &&?
This is Bitwise OR - | ?
And this is OR- ||?

I need to use thisone "|", because I am working with individual bits? Meaning that form 0b1000 0000 and 0b0000 1000 I will get 0b1000 1000?
 

eetech00

Joined Jun 8, 2013
4,004
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
How will the result be interpreted?
 
Top