Velocity Reviews > Concatenate bits, then convert to HEX

Concatenate bits, then convert to HEX

Abby
Guest
Posts: n/a

 10-28-2003
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

Peter Pichler
Guest
Posts: n/a

 10-29-2003
"Abby" <(E-Mail Removed)> wrote:
> 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

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

Mark McIntyre
Guest
Posts: n/a

 10-29-2003
On 28 Oct 2003 15:42:14 -0800, in comp.lang.c , http://www.velocityreviews.com/forums/(E-Mail Removed)
(Abby) wrote:

>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.

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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---