shifting bits

Hello, I want to make a function that shifts bits to the left and right and append them to the left or the right.I heard that is is called ROR and ROLL?
Example (to the left):
1
2
3
1100000
shift to the left with 2
0000011


Example (to the right):
1
2
3
0000111
shift to the right with 4
0111000


Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>


void rolBits(int v)
{
 int i;
 int mask = 1 << 31;
 
 for(i=1; i<=32; ++i)
 {
          putchar(((v & mask) == 0) ? '0' : '1');
          v <<= 1;
          if(i % 8 ==0 &&i <=32)
          putchar(' ');
 }
}

int main(void)
{
    unsigned int v;
    unsigned int b;
    int a;
    unsigned int c = 1 << (b-1) ;   
    
    
    printf("Give a positive integer:\t");
    scanf("%d",&v);
    printf("Give the amount of positions you want to shift:\t");
    scanf("%d",&b);
    printf("Wich side do you want to shift to? (L/R)");
    scanf("%s",&a);
    unsigned int g = (v << b) | (v >> b);
    
     printf("Binary this is:\t\t\t\t");
     rolBits(v);
    if(a == 'l' || a == 'L')
    {
       printf("\nThe bit has been shifted to the left:\t\t");
       rolBits(g);
        }
    
    else  
    {
    printf("Binary this is:\t\t\t");
    rolBits(v);
    printf("\nThe bit(s) has been shifted to the right\t\t");
    rolBits(k);
    }
    
    
    getchar();
    getchar();    
}



It looks as though function rolBits() is simply outputting the result in binary.

The actual rol operation is attempted here:
 
    unsigned int g = (v << b) | (v >> b);

If the integer is a 32-bit value, then this might be better:
 
    unsigned int g = (v << b) | (v >> (32-b));
Topic archived. No new replies allowed.