Hello everyone;
I've got a project that's asking me to input binary numbers and create a bunch of functions that'll print those binary, reverse them, shift to left and right and convert it into decimal.
I've managed to make the program print and reverse the numbers but I am a little lost on "shifting" the binary.
3. ShiftLeft
a. Shift the numbers in the array left (mimicking the shift left operator). Note: You will not use the
<< operator to do this. Remember to pad with 0’s. Only shift one spot per function call.
4. ShiftRight
a. Shift the numbers in the array right (mimicking the shift right operator). Note: You will not use
the >> operator to do this. Remember to pad with 0’s. Only shift one spot per function call.
I emailed prof. and she said;
"We are mimicking
one integer number by an 8 element array where each element holds a binary digit
making up that number... so if want to shift left, we simply need to move each
element in the array one space to the left."
So I guess calling the "shift left" function will turn 11001100 into 10011001 (or 567890 to 678905).
I wrote this ;
int left = 0;
int right = s-1;
while(left < right){
int temp = ar[left];
ar[left] = ar[right];
ar[right] = temp;
left++;
right--;
}
for reverse function and it works pretty good. Does anybody have an idea on how to handle this "shift left" thingy? I tried to store everything in temp and move them to left but...well, it failed. I suck at this thing :/
Thanks in advance for replies.