Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Binary to Decimal

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lmh86 - 02 Mar 2005 00:13 GMT
Should the following algorithm produce the binary equivalent of an inputed
decimal value?

----------
cout << "Decimal value: ";
cin >> Decimal;
Binary = 0;
Remainder = Decimal;
for(Power = 4; Power = -1; Power--) {
Remainder = Decimal - (pow(2,Power));
if(Remainder < 0) {
Binary = Binary * 10;
Remainder = Decimal;
}
if(Remainder >=0) {
Binary = ((Binary * 10) + 1);
Decimal = Remainder;
}
}
cout << "Binary value: " << Binary << "\n";
system("Pause");
break;
----------

Can anyone suggest any other methods that does not implement built-in
functions?

Please and thankyou.
Hendrik Schober - 03 Mar 2005 01:47 GMT
> [...]
> Can anyone suggest any other methods that does not implement built-in
> functions?

 This sounds like some homework, so I will
 hint you into some direction, but won't
 post code.
 When you have this:
   int i;
   std::cin >> i;
 the value ist stored in 'i' in binary form.
 (That's the only way the computer knows.)
 If you look at the lowest bit of 'i' (by
 masking off the others) you know it's value.
 After that, you can shift 'i's content to
 the right one position and thus inspect the
 next bit at the same position.
 Feel free to ask if anything in this is not
 clear.

> Please and thankyou.

 Schobi

Signature

SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.