### 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):
 ``123`` ``````1100000 shift to the left with 2 0000011``````

Example (to the right):
 ``123`` ``````0000111 shift to the right with 4 0111000``````

Here is my code so far:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include 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.