#include <iostream>
#include <stdio.h>
#include <stdbool.h>
#include <string>
struct dbit {
bool bit1;
bool bit2;
} ;
dbit BinaryAdd( bool num1, bool num2) {
dbit ret;
bool store;
bool carry;
//if statements for each possibility
if ( num1 == 1 && num2 == 1 ) { // 1 + 1
store = 0 ;
carry = 1 ; //carry the 1 over to the next digit
}
if ( num1 == 0 && num2 == 0 ) { // 0 + 0
store = 0 ;
carry = 0 ; //nothing
}
if ( num1 == 1 && num2 == 0 ) { //1 + 0
store = 1 ; //just 1
carry = 0 ;
}
if ( num1 == 0 && num2 == 1 ) { //0 + 1
store = 1 ; //just 1
carry = 0 ;
}
//put values into struct because we can't return two variables from a function
ret.bit1 = store;
ret.bit2 = carry;
//and return that value containing the store and carry
return ret;
}
int main( ) {
//create variables for input/output
bool input_bit1;
bool input_bit2;
dbit output;
//write title
std:: cout << "Binary Calculator\n " ;
//send user message to collect input
std:: cout << "Use the 'input' tab to choose which single-digit binary numbers to add\n " ;
std:: cin >> input_bit1;
std:: cin >> input_bit2;
//run function
output = BinaryAdd( input_bit1, input_bit2) ;
//write output
std:: cout << "Well, if we add " << std:: to_string ( input_bit1) << " and " << std:: to_string ( input_bit2) << ", we get " << std:: to_string ( output.bit2 ) << std:: to_string ( output.bit1 ) ;
//close program
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGJvb2wuaD4KI2luY2x1ZGUgPHN0cmluZz4KCnN0cnVjdCBkYml0IHsKCWJvb2wgYml0MTsKCWJvb2wgYml0MjsKfTsKCmRiaXQgQmluYXJ5QWRkKGJvb2wgbnVtMSwgYm9vbCBudW0yKSB7CglkYml0IHJldDsKCWJvb2wgc3RvcmU7Cglib29sIGNhcnJ5OwoJCgkvL2lmIHN0YXRlbWVudHMgZm9yIGVhY2ggcG9zc2liaWxpdHkKCWlmKG51bTEgPT0gMSAmJiBudW0yID09IDEpIHsgLy8gMSArIDEKCQlzdG9yZS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