2

I'm trying to get my program to work, where I shift bits to the left and add the shifted bits to the right. For example 00111000, if you shift it 4 positions to the left, the outcome should be 10000011. How can I make this work, I know that I need to use the bitwise OR. I have added the main function below.

#include <stdio.h>
#include <stdlib.h>

void printbits(int b){
    int i;
    int s = 8 * (sizeof b) - 1; /* 31 if int is 32 bits */
    for(i=s;i>=0;i--)
    putchar( b & 1<<i ? '1' : '0');
}



int main(){
    char dir; /* L=left R=right */
    int val, n, i;

    scanf("%d %d %c",&val, &n, &dir);
    printbits(val);putchar('\n');
    for (i=0; i<10; i++){
    if (dir=='L' || dir =='l')
      rotateLeft(&val, n);
    else
      rotateRight(&val,n);      
    printbits(val); putchar('\n');
    }
return;
}

This is the rotateLeft en rotateRight function.

#include <stdio.h>
#include <stdlib.h>

void rotateLeft(int *val, int N){
    int num = val[0];
    int pos = N;

    int result = num << pos;


}

void rotateRight(int *val, int N){
    int num = val[0];
    int pos = N;

    int result = num >> pos;


}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Sullivan
  • 64
  • 8
  • What do you mean by this " add the shifted bits to the right"? Shift right and left then sum them? – Bence Kaulics Oct 23 '16 at 13:04
  • [A similar question](http://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c). Note that if you're tossing about bits in the sign-bit of a signed `int`, you're walking on a pitch-black cliff edge with no flashlight. – WhozCraig Oct 23 '16 at 13:04
  • @BenceKaulics if you shift it to the left 4 positions, those 1's should be added to the right. I have a hard time explaining, sorry :S – Sullivan Oct 23 '16 at 13:20
  • See here: https://en.wikipedia.org/wiki/Circular_shift – Davislor Oct 23 '16 at 13:22
  • @Sullivan No problem there, I should have understood it by your example. – Bence Kaulics Oct 23 '16 at 14:09
  • Nitpick: Unless you know for a fact that the code will only ever run on systems with 8-bit bytes, you should multiply by `CHAR_BIT`, not `8`. – Justin Time - Reinstate Monica Oct 23 '16 at 17:40

3 Answers3

1

Here is a tested and non-optimized solution to complete your source code:

void rotateLeft(int *val, int N){
    unsigned int num = val[0];
    int pos = N;

    unsigned int part1 = num << pos;
    unsigned int part2 = (num >> ((sizeof(val[0])*CHAR_BIT)-pos));

    if (N != 0) {
        val[0] = part1 | part2;
    }
}

void rotateRight(int *val, int N){
    unsigned int num = val[0];
    int pos = N;

    unsigned int part1 = num >> pos;
    unsigned int part2 = (num << ((sizeof(val[0])*CHAR_BIT)-pos));

    if (N != 0) {
        val[0] = part1 | part2;
    }
}

To prevent automatic carry during the shift right, you have to consider value as unsigned int.

To prevent N = 0 interference, assign the result to the entry only when (N != 0). (See remark on post ROL / ROR on variable using inline assembly in Objective-C)

Community
  • 1
  • 1
J. Piquard
  • 1,665
  • 2
  • 12
  • 17
  • @piquard What does this part do? if (N != 0) { val[0] = part1 | part2; – Sullivan Oct 24 '16 at 10:06
  • @Sullivan the value in input is updated only when a ROR or ROL is ordered (meaning N != 0). NB: both two functions could be merge because a negative value for N will do the reverse operation. – J. Piquard Oct 24 '16 at 11:40
  • I see and why do you need to OR part 1 and part 2? – Sullivan Oct 24 '16 at 18:59
  • @Sullivan sorry, I didn't explain enough. The part1 is the number shifted (Left or Right) but stuffed by 0 and the part2 is the lost bits shifted to take place in front of part1 stuffed bits. The final OR merges the to set of bits. – J. Piquard Oct 24 '16 at 19:15
0
MSB = (n >> (NUM_OF_BITS_IN_INT - 1))
n = (n << 1) | MSB;

Left bit rotation of n by 1 bit.

You are just shrugging off the MSB but not adding it back at LSB position.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
0

use the functions in this link for performing the rotations:

https://en.wikipedia.org/wiki/Circular_shift

Vikram Baliga
  • 444
  • 5
  • 9