|
From: Stephen Jones on 30 Mar 2006 06:29 Hi.I am currently studying programming in C and have to write a program that converts a binary number (0 - 1111111) to decimal. Is there a way to do this without using string functions? Is there a function in any header files that I could use? Thanks :)
From: osmium on 30 Mar 2006 08:01 "Stephen Jones" writes: > Hi.I am currently studying programming in C and have to write a program > that converts a binary number (0 - 1111111) to decimal. Is there a way to > do this without using string functions? Is there a function in any header > files that I could use? Thanks :) Why would you want to use a function you found laying around? The point is for you to understand the relationship between binary and decimal, is that not so? Take an example: 100110 What does that mean? It means 1*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 0*2^0. Take it from there and write a program.
From: Stephen Jones on 30 Mar 2006 13:20 "osmium" <r124c4u102(a)comcast.net> wrote in message news:49234iFmlm32U1(a)individual.net... > "Stephen Jones" writes: > >> Hi.I am currently studying programming in C and have to write a program >> that converts a binary number (0 - 1111111) to decimal. Is there a way to >> do this without using string functions? Is there a function in any header >> files that I could use? Thanks :) > > Why would you want to use a function you found laying around? The point > is for you to understand the relationship between binary and decimal, is > that not so? > > Take an example: 100110 > What does that mean? It means > 1*2^5 + 0*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 0*2^0. > > Take it from there and write a program. >Thanks for responding to my question. I do understand the relationship >between binary and decimal numbers and know how to convert one to the >other. The problem however is this: in the course I am doing we have not >yet been taught string functions and are therefore not allowed to use them >in the program. If we WERE allowed to it would be a simple matter to split >the large number and work on each bit as you have shown in your example. Is >there a way I can split the large number without using string functions? >Thanx again :)
From: osmium on 30 Mar 2006 14:16 "Stephen Jones" writes: >>Thanks for responding to my question. I do understand the relationship >>between binary and decimal numbers and know how to convert one to the >>other. The problem however is this: in the course I am doing we have not >>yet been taught string functions and are therefore not allowed to use them >>in the program. If we WERE allowed to it would be a simple matter to split >>the large number and work on each bit as you have shown in your example. >>Is there a way I can split the large number without using string >>functions? I don't share your enthusiasm for a string function being especially useful. How about using the bitwise and and the shift instruction as a starting point? You could write a function to take integral powers of numbers, then call it with the parameters selected per the bit wise and. I would be sure the number operated on is unsigned before I started.
From: R. Scott Mellow on 30 Mar 2006 15:00 Stephen Jones wrote: >Thanks for responding to my question. I do understand the relationship >between binary and decimal numbers and know how to convert one to the >other. The problem however is this: in the course I am doing we have not >yet been taught string functions and are therefore not allowed to use them >in the program. If we WERE allowed to it would be a simple matter to split >the large number and work on each bit as you have shown in your example. Is >there a way I can split the large number without using string functions? >Thanx again :) Where are you getting the "binary number" from? The user? Can you show the code you have to get the "binary number" so that we can see what you have so far? -- Randy
|
Next
|
Last
Pages: 1 2 Prev: sorting the names in the ascending order Next: escape sequence - inserting into a ofstream |