I'm attempting to write a program which takes a string input of '0' and '1' from the user and then uses that given string to change each bit of another variable.
i.e.
char input[20];
unsigned short bits = 0;
printf("Enter a binary string of 16 bits.\n");
fgets(input, 20, stdin);
input[strlen(input) - 1] = '\0';
is what I have to get and store a two byte string from the user. I then want to take that string and manipulate the variable 'bits' to match what was entered.
So if the user entered 10011001 10011001 I would like to change the value of 'unsigned short bits' from all 0's to the matching value in binary.
My thinking right now is that I could loop through the input string and left shift 'bits' with each pass but as far as I know left shifting will always drop the left most bit and add a 0 as the right most bit. Is there a way to add a 1 as the left most bit with some bitwise command I don't know of so that if the loop sees a '1' in the string it adds a one to the rightmost bit?