Monday, July 9, 2007

How to swap the two nibbles in a byte ?

Try this


#include < stdio.h >

unsigned char swap_nibbles(unsigned char c)
{
unsigned char temp1, temp2;
temp1 = c & 0x0F;
temp2 = c & 0xF0;
temp1=temp1 << 4;
temp2=temp2 >> 4;

return(temp2|temp1); //adding the bits
}

int main(void)
{
char ch=0x34;
printf("\nThe exchanged value is %x",swap_nibbles(ch));
return 0;
}

30 comments:

  1. struct nibbles
    {
    int lnibble:4;
    int unibble:4;
    };
    union swap
    {
    char byte;
    struct nibbles nb;
    };
    int main()
    {
    union swap sw;
    int temp;
    sw.byte=0x12;
    printf("Before swap:%x\n",sw.byte);
    temp=sw.nb.lnibble;
    sw.nb.lnibble=sw.nb.unibble;
    sw.nb.unibble=temp;
    printf("After swap:%x",sw.byte);
    }

    ReplyDelete
  2. unsigned char nibbleSwap(unsigned char a)
    {
    return (a<<4) | (a>>4);
    }

    Masking operations in above example are not really needed.

    ReplyDelete
  3. These are really great ideas in concerning blogging.
    You have touched some nice factors here. Any way keep up wrinting.
    Here is my website ... www.shopmcanow.com

    ReplyDelete
  4. Thanks , I have just been searching for information
    approximately this subject for a long time and
    yours is the best I've found out till now. However, what about the conclusion? Are you positive in regards to the supply?
    Also see my web page > http://www.cafepress.com/lilbasser

    ReplyDelete
  5. Terrific article! That is the kind of info that should be shared around the
    web. Shame on Google for not positioning this submit higher!
    Come on over and talk over with my web site .
    Thank you =)
    Also see my webpage :: Toronto Office Furniture for less everyday

    ReplyDelete
  6. I am not sure the place you're getting your information, but good topic. I must spend a while studying more or figuring out more. Thanks for excellent info I used to be in search of this info for my mission.
    Also visit my homepage : bags of cotton candy

    ReplyDelete
  7. Howdy exceptional blog! Does running a blog such
    as this take a lot of work? I've virtually no expertise in computer programming however I was hoping to start my own blog in the near future. Anyhow, if you have any ideas or techniques for new blog owners please share. I understand this is off subject however I simply needed to ask. Appreciate it!
    Also visit my website :: will cleaning my dryer vent make my clothes dry faster carpet cleaning

    ReplyDelete
  8. Yes! Finally something about aani.
    Feel free to surf my web site : Contactez-Nous

    ReplyDelete
  9. After I originally left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I recieve 4 emails with the exact same
    comment. Perhaps there is a way you can remove me from that service?
    Thanks a lot!
    Feel free to visit my page ; hpv wart treatment

    ReplyDelete
  10. When someone writes an paragraph he/she retains the image of a user in his/her mind that how a user
    can be aware of it. So that's why this article is perfect. Thanks!
    Feel free to visit my webpage Holistic Health Care

    ReplyDelete
  11. This design is wicked! You obviously know how to keep a
    reader amused. Between your wit and your videos, I was
    almost moved to start my own blog (well, almost..
    .HaHa!) Fantastic job. I really enjoyed what you had to say, and more than that,
    how you presented it. Too cool!
    Review my site ... SanStreet

    ReplyDelete
  12. Spot on with this write-up, I actually feel this website needs far more attention.
    I'll probably be returning to see more, thanks for the information!
    Also see my web site: Kauai South Shore

    ReplyDelete
  13. I was curious if you ever considered changing the structure of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people
    could connect with it better. Youve got an awful lot of text for only having one or 2 pictures.
    Maybe you could space it out better?
    Here is my webpage :: unforked

    ReplyDelete
  14. Hi there very cool site!! Man .. Excellent .. Wonderful .
    . I will bookmark your blog and take the feeds also?
    I'm happy to search out so many helpful info right here within the publish, we'd like develop
    more techniques in this regard, thanks for sharing. . . . .
    .
    Look into my blog post gb retro 360 searchlight yellow

    ReplyDelete
  15. Hello would you mind stating which blog platform you're working with? I'm going
    to start my own blog in the near future but I'm having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I'm looking for something unique.
    P.S Apologies for getting off-topic but I had to ask!
    Feel free to surf my weblog :: Teamspeak and shoutcast servers

    ReplyDelete
  16. I don't even understand how I ended up here, but I believed this publish used to be great. I do not recognise who you're
    but definitely you are going to a well-known blogger if you happen to aren't already. Cheers!
    Feel free to visit my web blog :: Career Coaching For Women

    ReplyDelete
  17. Today, I went to the beach front with my kids. I found a
    sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed.

    There was a hermit crab inside and it pinched her
    ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!
    My web site : Popflops

    ReplyDelete
  18. I think the admin of this web page is really working hard in favor of his website, for the reason that here every
    stuff is quality based stuff.
    Here is my homepage 3d games

    ReplyDelete
  19. Neat blog! Is your theme custom made or did
    you download it from somewhere? A design like yours with
    a few simple tweeks would really make my blog jump out.

    Please let me know where you got your design. Bless you

    Feel free to surf to my web site: furnace troubleshooting chart

    ReplyDelete
  20. Gday! My name is Rogelio and I really just desired to say your weblog is awesome!
    It's amusing simply because I use to have a weblog that nearly had an identical website url: http://www.blogger.com/comment.g?blogID=7662544687317968079&postID=4812290997541906295 mine was only a few characters different. In any case, I am a big fan of your blogging site and if you ever would like a guest write-up make sure you email me at: eulagrant@gmail.com. I absolutely love writing!

    Have a look at my site short term apartment finding

    ReplyDelete
  21. Thаnks , I have juѕt bеen ѕearсhіng foг information approximatеly this topiс for agеs аnd youгs is the gгeateѕt I've came upon so far. However, what in regards to the conclusion? Are you positive in regards to the supply?

    Here is my site ... raspberry ketone

    ReplyDelete
  22. Thanks , I have јust beеn searching for infοrmatіon approxіmatеlу thiѕ tοpic fοr agеѕ anԁ
    yours is thе greatеst Ι've came upon so far. However, what in regards to the conclusion? Are you positive in regards to the supply?

    Feel free to surf to my web page :: raspberry ketone

    ReplyDelete
  23. My brother suggested I might like this blog. He was
    entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this info!
    Thanks!

    Here is my webpage kitchen appliances brands in vietnam

    ReplyDelete
  24. I do consider all of the concepts you have introduced
    on your post. They are very convincing and can certainly work.
    Still, the posts are too quick for beginners. Could you please prolong
    them a little from next time? Thanks for the post.


    Also visit my webpage; home renovation cost estimator spreadsheet

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Nice. Here's mine, using the fact that c variables are passed by value...

    #include < stdio.h >

    unsigned char swap_nibbles(unsigned char c)
    {
    unsigned char temp;
    temp = c & 0x0F;
    c = c & 0xF0;
    temp=temp << 4;
    c=c >> 4;

    return(c|temp); //adding the bits
    }

    int main(void)
    {
    char ch=0x34;
    printf("\nThe exchanged value is %x",swap_nibbles(ch));
    return 0;
    }

    ReplyDelete
  27. unsigned char swap_nibbles(unsigned char c)
    {
    return( c << 4 | c >> 4);
    }

    Check my Blog:
    azimchisty.blogspot.in

    ReplyDelete
  28. struct nibbles
    {
    int lnibble:4;
    int unibble:4;
    };
    union swap
    {
    char byte;
    struct nibbles nb;
    };
    int main()
    {
    union swap sw;
    int temp;
    sw.byte=0x12;
    printf("Before swap:%x\n",sw.byte);
    temp=sw.nb.lnibble;
    sw.nb.lnibble=sw.nb.unibble;
    sw.nb.unibble=temp;
    printf("After swap:%x",sw.byte);
    }


    this is better code thanks to help me and i have the another code for the same similar answer



    #include
    main()
    {
    char x=0x43;
    printf("%x",((x>>4) & 0x0f) | (x<<4) & 0x0f));
    }

    o/p=

    if we give x=43 then it will convert to the '34',how means?

    34 convert bytes means=00101011,
    0x0f(15)=00001111,

    x----------= 00101011
    (x>>4)---------= 00000010
    0x0f(15)-------= 00001111
    ---------
    ((x>>4)&0x0f)--= 00000010
    ---------

    x---------- = 00101011
    (x<<4)---------= 10110000
    0x0f(15)-------= 00001111
    ---------
    ((x>>4)&0x0f)--= 00000000
    ---------

    (x>>4)& 0x0f = 00000010
    (x<<4) & 0X0f = 00000000
    ----------
    doing (or) = 00000010 = 2
    ----------
    finally we will get " 2 " .so print that 2 in to the hexa decimal value



    please share me any question dought.

    Thanks.......

    ReplyDelete