ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
sorry mate im a bit of a newbie to java, i cant seem to find the code to just add two binary numbers together, there seems to be some sort of graphical binary program that im not really sure how its working (algerithum wise). Cheers.
Distribution: At home: Arch, OpenBSD, Solaris. At work: CentOS, Debian, Ubuntu
Posts: 3,625
Rep:
In a computer everything is stored as binary internally, so your questions doesn't make much sense to me. Do you mean that you have two strings of ones and zeros that you want to convert to binary and add? If so, I'd just iterate over the strings to construct a numeric variable (easy, if there's a one at position n from the last, add 2^n to your number), then add the numbers and convert the results back to a string of 1s and 0s to dispaly for the user.
btmiller is absolutely correct: you don't have to do anything special to "add in binary". You just ADD two numbers - nothing more!
To *read* a binary number, however, you'll need to convert the character string (e.g. "0110") into the corresponding int (e.g. "6"). Similarly, to *display* as a binary value, you'll need to convert the number into a string of ones and zeros.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.