The variables above only store values between 1 and 7 (including 1 and 7). My program asks the user to enter a number between 1 and 7. Upon entering a valid number, I execute a certain function based on the user's input and I reset the bits of the variables above back to 0 for future use. Very simple.
BUT, a problem occur when the user enters a value like 9. Apparently, 9 is being interpreted as 1. Many problems occur whenever the user enters a rubbish value like 589035 etc. My program goes haywire. I understand that the bits are possibly wrapping around. So how can I fix this problem please?
I just noticed. You should saturate the input before assigning its value to the bitfield. That's why my snippet says user_input. Obviously a 3-bit value can't possibly be greater than 7.