| | |
question about binary
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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?
- 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?
C Syntax (Toggle Plain Text)
/* 1. Get bits of unsigned char x and store into y. 2. Invert. */ #include <stdio.h> int to_binary(int); void main(void) { unsigned char x, y; puts("Enter a value for x:"); scanf("%d", &x); y = to_binary(x); } int to_binary(int n) { int r; r = n % 2; if (n >= 2) to_binary(n / 2); putchar('0' + r); return r; }
•
•
•
•
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.
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?
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.
![]() |
Similar Threads
- Im just beginning and need some help (PHP)
- IE7 pauses (IT Professionals' Lounge)
- Tree traversal (Computer Science)
- Binary Fraction conversion question (C)
- input bianary ASCII converted into Characters (C)
- Question about binary tree & heaps (Computer Science)
Other Threads in the C Forum
- Previous Thread: hw assignment help on selection & merge sort
- Next Thread: O notation
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






