Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

C basic "encryption"

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Any help is appreciated, thanks!
 
Saloon Keeper
Posts: 22648
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is where an IDE debugger is handly, since you can go through line by line and see what's happening. But if you don't have one or don't know how to use one, a few more printf's should help.

You don't need to cast the printf arguments as long as their type matches the format character, by the way.
 
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Tim Holloway
Saloon Keeper
Posts: 22648
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J Hiddleston
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
           ^~~

I'm not sure what I am doing wrong, sorry.
 
Tim Holloway
Saloon Keeper
Posts: 22648
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is not java. If you don't set an initial value on a variable, chances are you will get garbage. Garbage shifted is still garbage.
 
J Hiddleston
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:C is not java. If you don't set an initial value on a variable, chances are you will get garbage. Garbage shifted is still garbage.



I changed it to this :
ic = ic << offset;

I am no longer getting errors but my output is still incorrect. Any ideas?

 
John Matthews
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler is showing:
You are reversing the c input bits, but then not using the result.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic