Concatenate bits, then convert to HEX

A

Abby

Hi all,

I need advice on how to concatenate bits value.
I have 2 bytes of Hex value:

Byte1 = 0x33 --> 00110011
Byte2 = 0x90 --> 10010000

I want 10 bits value which compose of 2 bits(7th and 8th position)
from byte2 + all bits from byte1. So ... the 10 bits value will be
1000110011. How can I write a program to do this and how can I then
convert it into HEX value? I have no idea at all about how to deal
with binary value. Please give me some advice. Thank you so much for
all your help.
 
P

Peter Pichler

Abby said:
I need advice on how to concatenate bits value.
I have 2 bytes of Hex value:

Byte1 = 0x33 --> 00110011
Byte2 = 0x90 --> 10010000

I want 10 bits value which compose of 2 bits(7th and 8th position)
from byte2 + all bits from byte1. So ... the 10 bits value will be
1000110011.

value = Byte1 + ((Byte2 & 0xc0) << 2);

Byte2 & 0xc0 will mask the value with a bitmask 11000000, zeroing out
all except the topmost 2 bits. Shifting the result left by 2 places
these two bits where you want them. Add the other byte and you are done.
How can I write a program to do this and how can I then
convert it into HEX value? I have no idea at all about how to deal
with binary value. Please give me some advice. Thank you so much for
all your help.

I am not sure what you mean by "converting it into HEX value". C deals
with *values*, decimal, binary, hexadecimal etc are *representations*.
If you want to print the value in hexadecimal, use
printf("%x", (unsigned)value);
The cast may or may not be necessary, depending on the original type
of value.

Peter
 
M

Mark McIntyre

Byte1 = 0x33 --> 00110011
Byte2 = 0x90 --> 10010000
I want 10 bits value which compose of 2 bits(7th and 8th position)
from byte2 + all bits from byte1. So ... the 10 bits value will be
1000110011.

Think about the algorithm,
You first need mask off the bits you don't want from byte2 using &.
Then you need to copy it to a large-enough data type (needs to be at
least 10 bits wide)
Then you need to left-shift it by two bits using <<
then you need to then add byte1.
and how can I then convert it into HEX value?

All data is held in binary format on computers. You only see it in
hex, decimal, octal etc when you print it out. So the answer is, use
the right print specifier. In this case, I assume you expect the
answer to be 0x0233, so use the appropriate printf speficier for hex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top