Thread: Rotating bit patterns

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

    Rotating bit patterns

    Hi guys,

    I have a question about a program i'm writing as an assignment for school.

    It has to rotate bits and whenever bit fall out on one side the bit has to be send to the other side (i hope it's clear it's also called cyclic i think).

    in N = positive the bits need to shift to the left and when N in negative the bits shift to the right. So far this works. The only thing i don't know is how to catch the bits that are falling out and return them to the other side of the bit sequence.

    Hope this all is clear

    my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned int roteer(unsigned int waarde, int n);
    int main(void)
    {
       int uitkomst = roteer(4,-2);
       printf("%d ", uitkomst);
        
       system("pause");
       return 0;
    }
    
    unsigned int roteer(unsigned int waarde, int n)
    {
       
       int getal;
       int rechtsCounter;
       int counter = 0;
       
       if(n >= 0)
       {
         getal = waarde << n;
       }
       else 
       {
            rechtsCounter = n;
            while(rechtsCounter < 0)
            {
             counter++;
             rechtsCounter++;
            }
            rechtsCounter = counter;
            
            getal = waarde >> rechtsCounter;
       }
       
       return getal;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Say you want to shift four bits.

    x = shift (totalbits - 4) one way | shift (4) the other way

    Your program could also benefit from a function which prints all the bits as 0s and 1s.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM