LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2006, 07:21 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
adding binary numbers in java please help!!


hello all, i have two binary numbers that i want to add together from variables in java is there any way i can do it? i.e:

1010 + 1010 = 10100

please, please help i would be eternally grateful i have looked all over the web but found nothing of interest. cheers
 
Old 02-24-2006, 07:47 PM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Third link on Google:
Java Programming: Unit 1: Binary Addition
 
Old 02-25-2006, 10:26 AM   #3
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
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.
 
Old 02-25-2006, 01:29 PM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
 
Old 02-25-2006, 01:40 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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.

This page might be helpful:
http://mindprod.com/jgloss/binary.html

If we're not understanding you correctly, please post some sample code (or pseudo-code) to give us a better idea of what you're actually trying to do.

Thanx in advance ... and good luck!

Your .. PSM
 
Old 02-25-2006, 02:09 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Say you have 2 string bnum1, and bum2 that are suppose to be binary numbers.

int sum = Integer.parseInt(bnum1, 2) + Integer.parseInt(bnum2, 2);
 
Old 02-25-2006, 02:46 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You're absolutely right, 95se. I'm a bit embarrassed that I didn't just say it myself! Thank you!

Sincerely .. PSM
 
Old 02-25-2006, 04:15 PM   #8
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
I didn't realize that parseInt could handle binary strings -- that's good to know.
 
Old 02-25-2006, 10:38 PM   #9
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Edit: Just realized the link I posted was an actual full tutorial, not just the actual code.

Last edited by scuzzman; 02-25-2006 at 10:40 PM.
 
Old 02-28-2006, 03:16 PM   #10
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Original Poster
Rep: Reputation: 30
cheers you are all living legends thank you!!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Long Numbers In JAVA MRMadhav Programming 2 01-18-2006 02:03 PM
(Dail-up connection) - adding multi phone numbers per ISP medya Linux - Newbie 2 10-22-2005 11:57 AM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 05:25 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
C, read 8 bit octal numbers, convert to 24 bit binary bamalabs Programming 3 01-20-2004 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration