Concatenate bits, then convert to HEX

Discussion in 'C Programming' started by Abby, Oct 28, 2003.

  1. Abby

    Abby Guest

    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.
     
    Abby, Oct 28, 2003
    #1
    1. Advertising

  2. "Abby" <> 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
    > 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
     
    Peter Pichler, Oct 29, 2003
    #2
    1. Advertising

  3. On 28 Oct 2003 15:42:14 -0800, in comp.lang.c ,
    (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.


    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.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.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 =---
     
    Mark McIntyre, Oct 29, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,340
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    502
    Juha Autero
    Aug 19, 2003
  3. Replies:
    0
    Views:
    324
  4. Yuji
    Replies:
    3
    Views:
    170
    Sisyphus
    Sep 4, 2005
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    934
Loading...

Share This Page