944,116 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1128
  • C RSS
Mar 29th, 2006
0

question about binary

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Mar 29th, 2006
3

Re: question about binary

Quote 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.

Quote ...
- 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'.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Mar 29th, 2006
0

Re: question about binary

Ahh ok that clears up a lot of things. thx
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: InitCommonControlsEx
Next Thread in C Forum Timeline: O notation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC