0

Hi I currently have a binary string which I am shifting once to the left.

char bin[] = "01010001";

  printf("Binary number = %s \n", bin);
  printf("Binary number shifted by 1 to the left = %s \n", (bin+1) );

Output:

    Binary number = 01010001
    Binary number shifted by 1 to the left = 1010001

The shifted version should be: 10100010 rather than 1010001. Does anybody know why I am always losing the least significant bit?

Charles B
  • 95
  • 3
  • 14

1 Answers1

0

I guess your code is written in C?

Then you are not performing a shift operation on bits here.

Instead you declare a char array (pointer) and increment it's pointer position by one (via bin+1). The result is what you see. The leftmost character is being cut off.

For real shift operations use numeric variables, the shift operator (<<, >>) and print the variable in a binary representation (find a way e.g. here).

L. Hanke
  • 116
  • 1
  • 5