Thread: bitwise rotation

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    bitwise rotation

    I am preparing a program for 8bits bitwise rotation in which the input character will shift right by one bit everytime, eg.
    when input "aaa", the result will be "-80 88 44"

    And then I came out with the following and the result for inputing let say "aaa" will be
    "-80 -80 -80"
    and it waits for me to input another set of data, e.g. "aa"
    and the result is "88 88"
    ,and then I input another set and the result is "44 44" and so on...

    and I wonder how could I modify the program so that the shifting will be perfrom in one set of input and not with many set of inputs,

    char ch,i;

    printf("Please input character:");

    for(i=1; i <=8;i++)

    while((ch=getchar()) != '\n')

    printf("%d ", ch=ch>>i|ch<<(8-i));

    return (0);


    thanks

    I am thinking of adding another while loop in the last printf part but is not sure how could it be done.
    Last edited by sfff; 10-24-2009 at 08:46 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use an array, and then use a while loop to "walk" down the char's, one at a time.

    Start using code tags, please. In the advanced reply window, just highlight your program, and click on the # icon (right below the smilies pulldown menu in the header toolbar.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    i cannot use array in this project,
    moreover could you demonstratehowto walk downthe char using whileloop?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you can't use a char array, then you don't need to "walk" down the array element by element.

    Either I'm not clear from your assignment description, (very possibly), or you're not clear about your assignment. It's opaque to me, atm.

    Perhaps someone else will have an insight and will jump in.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The description is not clear, but I believe you may simply be wanting to swap the for loop and the while loop lines around, and you'd need to remove the "ch=" bit.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    sorry for the confusion,
    what I mean is that when I input character like "aaaaa", the program should output "-80 88 44 22" , which is bitwise rotation to the right 1 bit at a time.

    yet my coding above does the same thing, but it require me to press enter to move on to the next bitwise rotation. so the result for inputing "aaaa" is "-80 -80 -80 -80" and then i input again and it output "88 88 88 88" and "44 44 44 44" and so on, which is not I wanted...

    The requirement forbid me to use array processing so I canoot use that shortcut.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you have this for loop that does eight iterations, right? Why are you asking for a character for each iteration, if you want to use the same character each time?

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Quote Originally Posted by tabstop View Post
    So you have this for loop that does eight iterations, right? Why are you asking for a character for each iteration, if you want to use the same character each time?
    because I dont know what character will be used each time, the aaaa is just for example andit could be any letter number or signs.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sfff View Post
    because I dont know what character will be used each time, the aaaa is just for example andit could be any letter number or signs.
    So maybe you need to be clearer about what is supposed to happen. Right now it appears to me (based on what you are saying) that you want to read in one and only one letter, and will successively rotate it through eight positions. If that is not what you want, then tell us what it is that you do want.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by tabstop View Post
    So maybe you need to be clearer about what is supposed to happen. Right now it appears to me (based on what you are saying) that you want to read in one and only one letter, and will successively rotate it through eight positions. If that is not what you want, then tell us what it is that you do want.
    And if that is what you want, I've already told you exactly how to do it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise rotation
    By sfff in forum C Programming
    Replies: 4
    Last Post: 10-19-2009, 03:32 PM
  2. Image rotation (Bitwise operations?)
    By 127.0.0.1 in forum Game Programming
    Replies: 1
    Last Post: 05-29-2009, 12:45 PM
  3. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  4. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM