question about binary

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

question about binary

 
0
  #1
Mar 29th, 2006
Ok..I get how to do binary division and have written this simple program (everything except the output with putchar()). My goal, however, is to invert my input and print it out. I ran into a few problems trying to accomplish this:

- How do I get the one's complement operator ~ to work? I've tried inserting it here and there but all i get is smiley faces or some funky symbols.

- In putchar, what does '0' + r do?

  1. /*
  2. 1. Get bits of unsigned char x and store into y.
  3. 2. Invert.
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. int to_binary(int);
  9.  
  10. void main(void)
  11. {
  12. unsigned char x, y;
  13.  
  14. puts("Enter a value for x:");
  15. scanf("%d", &x);
  16. y = to_binary(x);
  17.  
  18. }
  19.  
  20. int to_binary(int n)
  21. {
  22. int r;
  23.  
  24. r = n % 2;
  25. if (n >= 2)
  26. to_binary(n / 2);
  27. putchar('0' + r);
  28. return r;
  29. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: question about binary

 
0
  #2
Mar 29th, 2006
Originally Posted by degamer106
- How do I get the one's complement operator ~ to work? I've tried inserting it here and there but all i get is smiley faces or some funky symbols.
The one's complement operator subtracts your number from the maximum possible unsigned integer that can be stored. For example, if you're using an unsigned char, the maximum possible unsigned char is 255. So ~ch is equivalent to 255 - ch. This is equivalent to flipping the value of all the bits in ch. If you're using an int, the maximum possible unsigned integer is equal to 4294967295, so ~x is equivalent to 4294967295 - x.

Try unsigned int x = 45; printf("%u", ~x);. I have a feeling you tried printing out bitwise complemented value as a character, instead of printing its decimal representation.

- In putchar, what does '0' + r do?
'0' is a way of writing the integer value that represents the character 0. This value (in the ASCII character set) is 48. So '0' + r is equivalent to writing 48 + r. Remember that a character is stored internally as an integer.

So if r is 1, then 48 + r gives the value 49, which is the ascii value for the character '1'.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: question about binary

 
0
  #3
Mar 29th, 2006
Ahh ok that clears up a lot of things. thx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC