Hey,
I can change decimal numbers to binary but I am having trouble doing it other way around...
Thank you.Code:int main() { int number, remainder; int highBitValue, numBits, bit; int i; printf("Please enter a decimal number: "); scanf("%d", &number); fflush(stdin); printf("The equivalent representation of %d in binary is ", number); /* find the largest power of 2 that fits into number */ highBitValue = 1; numBits = 0; remainder = number; while (remainder != 0) { remainder = remainder / 2; /* we keep track of both the highest digit value and the number of digits */ highBitValue = highBitValue * 2; numBits++; } remainder = number; /* avoid leading 0 bit */ highBitValue = highBitValue / 2; for (i=0; i<numBits; i++) { /* find the highest bit in remainder */ bit = remainder / highBitValue; printf("%d", bit); /* get ready for the next bit */ remainder = remainder - bit * highBitValue; highBitValue = highBitValue / 2; }



LinkBack URL
About LinkBacks


