Binary Addition with the Carry Bit

This is probably going to be a stupid question but I'm stuck on it and hopefully someone here can help me. I'm to take 2 integers provided by the command line and then convert them to 32bit binary numbers. Then numbers are added. The result should come out to something like this.
00000000000000000000000000011110 carry
00000000000000000000000000011110 a - first operand
00000000000000000000000000110010 b - second operand
00000000000000000000000001010000 a+b - result
My problem is I can't figure out how to get the carry bit to interact with the other operands. Here's what I have so far. Sorry if it gets messed up the formatting.
import java.io.*;
public class BitAdder {
     public static void main(String[] args) {
     if (args.length != 2) {
          System.out.println("SYNTAX: java BitAdder Operand1 Operand2");
          System.exit(1);
     int first = Integer.parseInt(args[0]);
     int second = Integer.parseInt(args[1]);
     int result;
     int carry;
     int one = 1;
     int zero = 0;
     //sFirst and sSecond are String versions of the binary numbers. first and second are
     //int versions of those same numbers.
     String sFirst = Integer.toBinaryString(first);
     String sSecond = Integer.toBinaryString(second);
     int firstLength = sFirst.length();
     int secondLength = sSecond.length();
     first = Integer.parseInt(sFirst);
     second = Integer.parseInt(sSecond);
     int fUnused = 32 - firstLength;
     int sUnused = 32 - secondLength;
     for (int i = 0; i < fUnused; i++) {
          sFirst = "0" + sFirst;
     for (int i = 0; i < sUnused; i++) {
          sSecond = "0" + sSecond;
     BufferedReader fRead;
     BufferedReader sRead;
     fRead = new BufferedReader(new StringReader(sFirst));
     sRead = new BufferedReader(new StringReader(sSecond));
     try {
          fRead.mark(35);
          sRead.mark(35);
          //for whatever reason the Reader sees 0's as 48 and 1's as 49
          for (int i = 0; i < 32; i++) {
               int carFir = fRead.read();
               int carSec = sRead.read();
               //both are 0
               if( carFir == 48 && carSec == 48 ) {
                    System.out.print("0");
               // one is 1, the other 0
               else if (carFir == 49 && carSec == 48 || carFir == 48 && carSec == 49) {
                    System.out.print("0");
               //both are 1's
               else if (carFir == 49 && carSec == 49) {
                    System.out.print("1");
               //something has gone wrong and the results aren't 1's and 0's anymore
               else {
                    System.out.print("wtf");
          System.out.print(" carry");
          fRead.reset();
          sRead.reset();
          System.out.println("");
     catch (IOException ioe) {
          System.out.println("Exception " + ioe);
     System.out.println(" " + sFirst + " a - first operand");
     System.out.println(" " + sSecond + " b - second operand");
     try {
          System.out.print(" ");
          //for whatever reason the Reader sees 0's as 48 and 1's as 49
          for (int i = 0; i < 32; i++) {
               int firstIn = fRead.read();
               int secondIn = sRead.read();
               //both are 0
               if( firstIn == 48 && secondIn == 48 ) {
                    System.out.print("0");
               // one is 1, the other 0
               else if (firstIn == 49 && secondIn == 48 || firstIn == 48 && secondIn == 49) {
                    System.out.print("1");
               //both are 1's
               else if (firstIn == 49 && secondIn == 49) {
                    System.out.print("0");
               //something has gone wrong and the results aren't 1's and 0's anymore
               else {
                    System.out.print("wtf");
          System.out.print(" a+b - result");
     catch (IOException ioe) {
          System.out.println("Exception " + ioe);
Any help would be greatly appreciated. Thanks in advance.

In your example the second bit in carry should be 0. You get 48='0' and 49='1' because you read ASCII characters.
int carry = 0;
for (int i = 0; i < 32; i++) {
   int first = 0;
   int second = 0;
   int firstIn = fRead.read();
   if (firstIn = 49)
      first = 1;
   int secondIn = sRead.read();
   if (secondIn = 49)
      second = 1;
   int add = first + second + carry;
      //both are 0
   if(add == 0) {
      carry = 0;
      System.out.print("0");
      // one is 1, the other 0
    else if (add == 1) {
      carry = 0;
       System.out.print("1");
       //both are 1's
    else if (add == 2) {
       carry = 1;
       System.out.print("0");
       //both are 1's and carry is 1
    else if (add == 3) {
       carry = 1;
       System.out.print("1");

Similar Messages

  • Is there an HDR plug in that will work with the 64 bit version of PSE 13?

    Is there an HDR plug in that will work with the 64 bit version of PSE 13?  I was hoping that NIK HDR Pro2 would work since Elements 13 is now 64 bit, but unfortunately, that is not the case.

    julesoir wrote:
    The internal speakers work fine when I boot in Windows, but they don't appear as an option in the System Preferences Output selection when in OS X. Does anyone know what command I could use to tell it to use the internal speakers instead?
    Ideally it would be great if I could then just click between output options in System Preferences, but any solution at all would be greatly appreciated.
    I am not sure if this will help, but if you haven't already checked the settings in the Audio MIDI Setup utility, try 'em out. Most people don't know that utility exists, but it has additional audio routing/muting options that are not in Sound system preferences.

  • Running in 64 bit mode with the 32 bit Oracle client installed

    I have seen many postings on this issue but none of the suggested solutions have helped me.
    I am running visual studio 2010 professional with SP1 on windows 7 64 bit machine. When I try to run a web service locally and call it with SoapUI, I get the following error message during OracleConnection.Open().
    Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.
    I have tried setting up the build configuration and the project settings to target 32 bit but that has not helped. I have also tried removing the reference to System.Data.OracleClient that shows being under Program Files (x86) and add a new reference to the dll under GAC_64. That too did not work.
    I only have the 64 bit client installed on my machine.
    Any help will be appreciated.
    Thank you

    1. Visual Studio any version is 32 bit ONLY irrespective of hardware or OS bits.  So to DEVELOP with Oracle Client you MUST install 32 bit Oracle ODAC components. Task manager will show devenv.exe *32 (to confirm 32 bit Visual Studio application).
    2. You can run your web application inside IIS/IIS Express in your PC which independently can be either 32bit or 64bit.  If it is 64 bit, you MUST also install 64 bit Oracle ODAC components which means you'll have to install both 32bit and 64bit in your 64 bit Win7 running on 64bit hardware.
    3. In Visual Studio if you compile with "AnyCPU" as target, the resultant binaries (Web/NonWeb) can be both 32bit or 64bit.  So if target PC is "AnyCPU" or "x64", you MUST install Oracle ODAC 64 bit in the target OS if that target OS is 64bit.  You don't have to install 32bit ODAC in the target PC.
    4. One more catch to above scenario is the target OS could be 64bit simulated OS on a 32bit hardware. In such case you MUST install both 32bit and 64bit ODAC components.
    So while you ask your network team to download ODP.NET/ODAC Components make sure you ask them to download both 32bit and 64bit. Total nearly 600MB.
    Hope this helps many people out there.

  • Iam from Yemen we have CDMA carrier called Yemen mobile I bought an iPad from the US CDMA works with Verizon when I got to Yemen doesn't work with the carrier Yemen mobile and  both carriers verizon and Yemen mobile work by the CDMA system don't know whey

    Iam from Yemen we have CDMA carrier called Yemen mobile I bought an iPad from the US CDMA works with Verizon when I got to Yemen doesn't work with the carrier Yemen mobile and  both carriers verizon and Yemen mobile work by the CDMA system don't know whey can anyone help me please..

    A Verizon-model iPad can only work on CDMA with Verizon. You cannot use it with any other carrier. Apple, to my knowledge, has not released the iPad in Yemen, so there is no iPad you can use with any carrier there that works only on CDMA. You would need to find a GSM carrier and then get an unlocked GSM iPad.
    Regards.

  • I have an iphone 3gs, i bought it with the carrier Orange in Mauritius and i would like to know if it is possible to unlock the iphone so that i can use another carrier.

    i have an iphone 3gs, i bought it with the carrier Orange in Mauritius and i would like to know if it is possible to unlock the iphone so that i can use another carrier. thanks

    Only the carrier can authorise unlocking. You need to contact them.

  • Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

    Hello guys,
                Problem has been solved -
    Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.
    1. If you install 64 bit oracle this is not an issue. Install 64 bit oracle.
    2. Download odp.net 32 bit  from below link:-
        ODP.NET Managed Driver Beta Download&lt;/title&gt;&lt;meta name=&quot;Title&quot; content=&quot;ODP.NET Managed Driver B…
    3. installed the odp.net.
    4. open the visual studio 2010.
    5.Select new - project - asp.net web application - project name -ok
    6.select tools- > connect to database- then follow the following steps
                             i>  user name - your user name/client username (example -- hr)
                             ii> password -- enter your password/client password  (example- hr)
                            iii>select connection type -- if you want to connect client database then- select EZ connect other wise select tns
                            iv>test the connection- then you will get connection succeed.
                            v> then click ok
    Thanks
    Chandrashekhar

    You do not need to do anything in VS.  You do need to configure both tnsnames.ora files, because the drivers do not share any data.
    Please see:
    http://sqlblog.com/blogs/jorg_klein/archive/2011/06/09/ssis-connect-to-oracle-on-a-64-bit-machine.aspx

  • This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

    hi friends ,
    My report Fetch from the orcle database , we installed Oracle 10g and 11g - clent 32 -bit on win 2k8 -64 bit machine .while design time runing fine,but ofter depolyement im facing below issue.
    In error has occurred during report processing. (rsProcessingAborted)
    The execution failed for the shared data set 'abc'. (rsDataSetExecutionError)
    Cannot create a connection to data source ' Data source for shared dataset'. (rsErrorOpeningConnection)
    Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.
    An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
      Any help is greatly appreciated, I have tried couple of solutions going through some threads online but no use and place dt tell install 1og r 11g 64-bit

    Hi,
    Based on the error message, we should confirm whether Oracle client tool has been installed on the Report Server at the beginning. If the Report Server installed is 64-bit, we should also install a 64-bit Oracle client tool on the Report Server. Besides, we
    can also try to bypass tnsnames.ora file when connecting to Oracle by specifying all the information in the data source instead of using the TNS alias to connect
    to the Oracle database. Please see the sample below:
    Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST= server01.mydomain.com)(PORT=1521)))(CONNECT_DATA=(SID=OracleDB)(SERVER=DEDICATED)));
    Reference:http://blogs.msdn.com/b/dataaccesstechnologies/archive/2010/06/30/ora-12154-tns-could-not-resolve-the-connect-identifier-specified-error-while-creating-a-linked-server-to-oracle.aspx
    Hope this helps.
    Regards,
    Heidi Duan
    Heidi Duan
    TechNet Community Support

  • I want to buy an ipad for someone that lives in Brazil. Which carrier should i buy so that it will work with the carriers in Brazil?

    I want to buy an ipad for someone that lives in Brazil. Which carrier should i buy so that it will work with the carriers in Brazil?

    Look, any officially unlocked GSM iPhone will work in India. You purchase from Dubai, no FaceTime, and Dubai is part of the UAE. You purchase anywhere but India, and you will have NO warranty or support. Frankly, doesn't make any sense to me why you want to do this...and don't bother telling me you save hundreds of dollars...still doesn't make any sense. Do a forum search here. We get posts daily, from users in India, who didn't purchase their iPhones in India...guess what? They all have problems. You want to join that bunch? It's your money, have at it.

  • How to use TYPE addition with the OPEN DATASET ?

    Hi,
    How to use TYPE addition with the OPEN DATASET and what is the use ?
    For Example:
    OPEN DATASET 'test.dat'
      TYPE 'lrecl=80, blksize=8000, recfm=FB'
      FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
    Also, if I wish to do the same for a .CSV file how can we do it.
    Thanks,
    -Sid

    Hi,
    OPEN DATASET 'test.dat'
      TYPE 'lrecl=80, blksize=8000, recfm=FB'
      FOR OUTPUT IN TEXT MODE ENCODING DEFAULT
    For more information press F1 on OPEN DATASET.
    Thanks

  • I cannot get my Mac Pro (Original) MacPro1,1 to boot with the 64 Bit kernel in 10.6.8

    I cannot get my Mac Pro (Original) MacPro1,1 to boot with the 64 Bit kernel in 10.6.8 (this is a 2 x 3Dual-Core Intel Xeon machine).
    Firmware = MP11.005C.B08 (EFI 1.2) 1.7f10 (SMC 1.1)
    Will it not  run the 64 bit kernel?
    Is there a firmware upgrade to allow this?
    Should I bother?
    It won't boot with the Key 6 & 4 method.
    or using Terminal command;
    sudo systemsetup -setkernelbootarchitecture x86_64
    Any suggestions?
    Thanks in advance...
    Eric

    Because it can't and doesn't have a 64-bit supported firmware.
    UEFI (EFI64) was not finalized and adopted until very late 2007 and introduced in 2008, by Apple, Intel, other members of EFI Group and Unified EFI.  The Mac Pro Mid-2010 was  the first time though that 64-bit mode was the default.
    Does it matter? not really. you can't install 48GB RAM anyway, while there are some improvements from 64-bit kernal.
    Also can't boot Windows 7 64-bit DVD to install properly. But you can run Windows 64-bit kernel because you have a 64-bit Xeon.
    Macs that use 64-bit kernel
    64-bit kernel mode Snow Leopard
    Booting with 32-bit or 64-bit kernel
    Mac OS X- 64-bit kernel questions
    http://macperformanceguide.com/SnowLeopard-64bit.html

  • Hi guys, i just got my iPhone 4s and sometimes the signal is lost and i have to restart the phone in order to get reception again. do you thing there is a problem with the phone or with the carrier? thanks

    Hi guys, i just got my iPhone 4s and sometimes the signal is lost and i have to restart the phone in order to get reception again. do you thing there is a problem with the phone or with the carrier? thanks

    I have the exact same problem. Called my operator who told me it was a hardware issue and Apple is working on a fix...

  • My connection with the carrier cames and goes

    The connection with the carrier cames and goes randomly....

    Turn off the powersaving in the WLAN settings. Tools-> settings-> connection-> WLAN-> options-> advanced settings->yes-> powersaving-> off.
    ‡Thank you for hitting the Blue/Green Star button‡
    N8-00 RM 596 V:111.030.0609; E71-1(05) RM 346 V: 500.21.009

  • HT201328 I CONTACTED WITH THE CARRIER BUT THEY SAID BECAUSE I GOT THIS IPHONE MOBILE SET FROM THE INSURANCE COMPANY SO THEY CANT UNLOCK THE SET, THEY TOLD ME TO CONTACT WITH THE APPLE

    I CONTACTED WITH THE CARRIER BUT THEY SAID BECAUSE I GOT THIS IPHONE MOBILE SET FROM THE INSURANCE COMPANY SO THEY CANT UNLOCK THE SET, THEY TOLD ME TO CONTACT WITH THE APPLE

    WHY ARE YOU YELLING AT US!
    You are not addressing Apple here, just users like you.
    Go here, find the number and call Apple:
    http://support.apple.com/kb/HE57

  • Opening a binary file with the right extension

    Hi everyone,
    I developed a real simple VI that enables me to read the first five bytes of a binary file to get its signature (ASCII) and verify if its signature matches with the expected extensions. If yes, another VI is called, and if not a 2 button message error is displayed : if the User chooses OK, then a new dialog box opens up to pick another file ; and if the user chooses Cancel, the same dialog box opens up rather than escaping the VI.
    It is due to the while loop (VI below) but i don't see how I can handle this without it. Till the user hasn't chosen the right extension, it can't escape which is a problem! The true case consists in calling another VI, and the first step of the stacked sequence consists in displaying a two button error message.
    If you have any ideas, I'm a bit new! Thanks !!
    Solved!
    Go to Solution.

    Thanks for your replies!
    I have tried your solution CoqRouge, filtering the extensions. But it seems that you can choose between different filters when the dialog box opens up, and then choose any kind of extensions.
    @RavensFan : I cannot load the VIs attached in this thread because I am using an older version of Labview
    @MikeS81 : Which negative ouput?
    Attachments:
    ptw_show_do_while.vi ‏20 KB

  • I bought an IPhone 4s at an Apple store whilst living in China with the carrier China Mobile. It is a prepaid phone (no plan). Can I use it in another country by changing the SIM card? Or does it need to be unlocked? Can it be unlocked?

    I lived in China when I bought the IPhone 4s with China Mobile. It is prepaid so I have to put money on my phone when it runs out. Now I'm living in Mexico and I was wondering if I could keep using my IPhone with the mexican carrier TELCEL. Can I just insert a new SIM card from this company? Or does it have to be unlocked? BTW I had no contract with China Mobile. Thank you in advance:)

    Could it be because the SIM card got damaged while I cut it to fit the IPhone?
    Very likely. Hard to believe the carrier in Mexico doesn't have a micro-SIM card available.
    Or somehow the Chinese carrier found out and locked it?
    No, which is not possible.
    What can I do? Get a new SIM and see if it works again?
    Get a micro-SIM from the carrier in Mexico that you don't have to manually cut down. If that carrier doesn't provide one, switch to another carrier that is not so low-budget.

Maybe you are looking for