Bit concatenation in C++
-
I cannot use shifting here, because i am considering bits in the integer form. if i shift, data will change know. Suggest any other way to store bits in C/C++
though richard solutions is best.. i am suggesting simpler approach for this in my college days :-)
int result = i*100 + j *10 + k;
Jokes apart! are you looking for it's integer equivalent. i.e. say you have bits like this i=1,j=0,k=0 for which decimal equivalent is 4 ( binary of 100 = 4 i.e. 22 + 0 + 0 )
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
I am implementing turbo encoder, I will give some data as a input and i am converting it into the binary, since i don't know how to store and which data type for binary values, i have stored those binary values as integers only. Now i need to concatenate three bits at a time, I need only 1's and 0's not the integer values. If i shift these values i will get more than 1.
-
though richard solutions is best.. i am suggesting simpler approach for this in my college days :-)
int result = i*100 + j *10 + k;
Jokes apart! are you looking for it's integer equivalent. i.e. say you have bits like this i=1,j=0,k=0 for which decimal equivalent is 4 ( binary of 100 = 4 i.e. 22 + 0 + 0 )
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
No i dont need the decimal value of binary. I need to that as it is as 1's and 0's. if i have u=1; p=0; q=1; Then i need result = {u,p,q} = "101" (its neither decimal '5' nor decimal 'one hundred one') Just i need to concatenate bits. or else help me how to store binary values and which is the data type for that.
-
No i dont need the decimal value of binary. I need to that as it is as 1's and 0's. if i have u=1; p=0; q=1; Then i need result = {u,p,q} = "101" (its neither decimal '5' nor decimal 'one hundred one') Just i need to concatenate bits. or else help me how to store binary values and which is the data type for that.
if you looking for string value as i understand by double quote used by you, you can use sprintf or sprintf_s. now if you want to store the binary value resulting from you operation (as you mentioned they are only 3 bit coming), you can use char datatype (1 byte) and store the decimal equivalent of it there, internally it store its binary equivalent only, for say you got 101 which decimal equivalent is 5 and when you store same in char variable, it binary representation would be 00000100
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
I am implementing turbo encoder, I will give some data as a input and i am converting it into the binary, since i don't know how to store and which data type for binary values, i have stored those binary values as integers only. Now i need to concatenate three bits at a time, I need only 1's and 0's not the integer values. If i shift these values i will get more than 1.
Manoj7390 wrote:
If i shift these values i will get more than 1
Of course. Now look at decimal case: if you 'concatenate' the digits
1
,5
,7
then you get the number157
that is no more in the0-9
range. An alternative would be using strings, that is, you concatenate the character representations of the digits into a string ('1'
','5'
,'7'
=>"157"
). You may apply exactly the same argument to binary (that is base2
) digits: if you concatenate1
,0
,0
you get the number101
, that is decimal5
, no more in the0-1
range. The string concatenation method would give you"101"
. You may find interesting the std::bitset[^] class. However it does not prevent you from gaining a proper understanding of numerical bases (see, for instance, Numeral System at Wikipedia[^]) and how numbers are stored in computers.Veni, vidi, vici.
-
if you looking for string value as i understand by double quote used by you, you can use sprintf or sprintf_s. now if you want to store the binary value resulting from you operation (as you mentioned they are only 3 bit coming), you can use char datatype (1 byte) and store the decimal equivalent of it there, internally it store its binary equivalent only, for say you got 101 which decimal equivalent is 5 and when you store same in char variable, it binary representation would be 00000100
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
if you looking for string value as i understand by double quote used by you, you can use sprintf or sprintf_s. now if you want to store the binary value resulting from you operation (as you mentioned they are only 3 bit coming), you can use char datatype (1 byte) and store the decimal equivalent of it there, internally it store its binary equivalent only, for say you got 101 which decimal equivalent is 5 and when you store same in char variable, it binary representation would be 00000100
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Manoj7390 wrote:
Suggest any other way to store bits in C/C++
This has nothing to do with C/C++, it's how computers work. If you will not use the shift operators to store the three bits together then you cannot store them.
Use the best guess
Richard MacCutchan wrote:
If you will not use the shift operators to store the three bits together then you cannot store them.
int Answer = u * 4 + p * 2 + q
-
Can you please suggest me a program to convert a string into binary, that store in char data type.
char * p = "101";
char * pEnd;
int val = strtol(p, &pEnd, 2);
char charval = (char)val;this may fail if p is more than 7 bits long (since that could overflow a signed char).
-
Richard MacCutchan wrote:
If you will not use the shift operators to store the three bits together then you cannot store them.
int Answer = u * 4 + p * 2 + q
-
But i need to concatenate the bits which are in different variables. how to concatenate those.. Since i cannot use shift operator here because of binary.
Manoj7390 wrote:
i cannot use shift operator here because of binary.
What on earth are you talking about? I have shown you exactly how to concatenate these fields into a three bit value. If you really cannot understand that simple operation then I don't think you are going to get very far with this project.
Use the best guess
-
Which the compiler will almost certainly optimise into shift operations.
Use the best guess
possibly. but that extends the definition of "you" quite a bit. int Answer = u + u + u + u + p + p + q;
-
Hi. I have written the program for turbo encoder, for every single bit, it gives three bits as output and I am storing it as a integer. Now the problem is how to concatenate these bits in the integer form. for example... int u = 1; int p = 0; int q = 1; Answer = 101;
Something like:
int answer = (u*100) + (p*10) + q;
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
possibly. but that extends the definition of "you" quite a bit. int Answer = u + u + u + u + p + p + q;
-
Chris Losinger wrote:
that extends the definition of "you" quite a bit.
:confused:
Use the best guess
you originally wrote "If you will not use the shift operators to store the three bits together then you cannot store them." that's the "you". i think it's reasonable to differentiate between what the programmer writes and what the compiler does.
-
you originally wrote "If you will not use the shift operators to store the three bits together then you cannot store them." that's the "you". i think it's reasonable to differentiate between what the programmer writes and what the compiler does.