I need to create a program that takes in two ints, and a start character, and "encrypts them" by reversing the characters binary representation, converting that to an integer, left shifting the int by the first given integer, and right shifting the int by the second given integer, and then returning the value. The program must then take a cipher value and undo the encryption based on two given ints for the shifts. This is what I have so far and it makes sense in my head but is not working in practice.
Tim Holloway wrote:Well, this is where an IDE debugger is handy
As is a better compiler. Or at least turn up the warning level on the one you are using; this is what I get when I compile with gcc and the -Wall 'all warnings' option:
The 'statement with no effect' warnings are because the << and >> should be <<= and >>= to assign the result rather than just calculate it.
posted 1 week ago
Also, the input to encrypt() (if I understand correctly) is 'A', whose ASCII value is 65. But you don't store the return value from encrypt(), and supply 65 (= 'A') as the input to decrypt().
Presumably you want to supply the result of encrypt() as the input to decrypt(), then check that the output (return value) of decrypt() is the value you started off with. Something like:
Oh, definitely. Notice all those "statement has no effect" errors. They're absolutely correct. You're using a shift operator on two values and discarding the results of the operation. I don't think that's what you want to do.
I would have noticed that earlier, except that C++ uses the shift operators for input/output and got my mental wires crossed. The compiler, on the other hand, did not.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.
posted 1 week ago
Thank you for the answers, I've attempted to use the shift with the equals but am getting this error :
main.c: In function ‘encrypt’:
main.c:27:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<<=’ token
int ic <<= start;