JP Morgan Interview Question
Applications DevelopersCountry: United States
#include <stdio.h>
#define len(s) sizeof(s)/sizeof(s[0]) - 1
#define MAX(a,b) ((a) > (b)) ? (a) : (b)
void addBitStrings(char *op1, int op1_s, char *op2, int op2_s, int carry, char *result, int result_s){
if (result_s < 0) return;
int first = 0;
int second = 0;
if (op1_s >= 0)
first = op1[op1_s] - '0';
if (op2_s >= 0)
second = op2[op2_s] - '0';
result[result_s] = (first ^ second ^ carry) + '0';
carry = (first & second) | (second & carry) | (carry & first);
addBitStrings(op1, op1_s - 1, op2, op2_s - 1, carry, result, result_s - 1);
}
int main(){
char op1[] = "1100011";
char op2[] = "10";
char *result;
int max = MAX(len(op1), len(op2)) + 1;
result = (char*)calloc(sizeof(char)*max + 1, 1);
printf("%s", addBitStrings(op1, len(op1) - 1, op2, len(op2) - 1, 0, result, max - 1));
return 0;
}
/* Of course use StringBuilder instead of just String. Accidentally implemented turning int into String also :D
*/
public class BinaryStringSum {
public static void main(String[] args) {
System.out.println(Integer.valueOf(intToString(32623634),2));
System.out.println(Integer.valueOf(intToString(43442642),2));
System.out.println(Integer.valueOf(binaryStringSum(46263, 43734),2));
System.out.println(46263+43734);
}
public static String binaryStringSum(int a, int b) {
String bS , aS, res = "";
bS = intToString(b);
aS = intToString(a);
int i = bS.length() > aS.length() ? bS.length()-1 : aS.length()-1, c = 0;
while(i >= 0) {
int bit1 = aS.length() > i ? aS.charAt(i) - '0' : 0;
int bit2 = bS.length() > i ? bS.charAt(i) - '0' : 0;
int sum = bit1 + bit2 + c;
c = (2 & sum) != 0 ? 1 : 0;
res = ((sum & 1) != 0 ? 1 : 0) + res;
i--;
}
if(c == 1) res = 1 + res;
return res;
}
public static String intToString(int num) {
int mask = 1 << 30;
String numS = "";
if(num == 0) return "0";
else {
while((num&mask) == 0) {
mask = mask >> 1;
}
while(mask!=0) {
numS = numS + ((num&mask)!= 0 ? 1 : 0);
mask = mask >> 1;
}
}
return numS;
}
}
char *decimal_to_binary(int n)
{
int c, d, count;
char *pointer;
count = 0;
pointer = (char*)malloc(32+1);
if ( pointer == NULL )
exit(EXIT_FAILURE);
for ( c = 31 ; c >= 0 ; c-- )
{
d = n >> c;
if ( d & 1 )
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';
count++;
}
*(pointer+count) = '\0';
return pointer;
}
int fromBinary(char *s) {
return (int) strtol(s, NULL, 2);
}
char * sum(char * firstnumber, char * secondnumber){
int firstnum = fromBinary(firstnumber);
int secondnum = fromBinary(secondnumber);
int value = firstnum + secondnum;
return decimal_to_binary(value);
}
String addBinaryNumbers(String xstr, String ystr) {
String result="";
int r=0, x=0, y=0;
int carryover[]=new int[31];
//
for (int k=xstr.length()-1, bit = 0; k>=0; k--, bit++) { if(xstr.charAt(k)=='1') { x+=(int) Math.pow(2, bit); } }
for (int k=ystr.length()-1, bit = 0; k>=0; k--, bit++) { if(ystr.charAt(k)=='1') { y+=(int) Math.pow(2, bit); } }
//
for (int bit = 0; bit <31; bit++) {
// cases
if( carryover[bit]==1 ) {
if( is_set(x, bit) && !is_set(y, bit) ) { carryover[bit+1]=1; }
if( !is_set(x, bit) && is_set(y, bit) ) { carryover[bit+1]=1; }
if( is_set(x, bit) && is_set(y, bit) ) { r=set_bit(r, bit); carryover[bit+1]=1; }
if( !is_set(x, bit) && !is_set(y, bit) ) { r=set_bit(r, bit); }
}else{
if( is_set(x, bit) && !is_set(y, bit) ) { r=set_bit(r, bit); }
if( !is_set(x, bit) && is_set(y, bit) ) { r=set_bit(r, bit); }
if( is_set(x, bit) && is_set(y, bit) ) { carryover[bit+1]=1; }
if( !is_set(x, bit) && !is_set(y, bit) ) { }
}
}
result=Integer.toString(r,2);
return result;
}
/** set bit[i] to 1 */
int set_bit(int y, int b) {
return y | (1 << b); // set the bit on, logical OR
}
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int str_to_int(char *s)
{
int len = strlen(s);
int i = 0;
int res = 0;
while(len--) {
if (s[len] == '1')
res += 1 << i;
i++;
}
return res;
}
char *int_to_bin(int n)
{
int i;
char *res = malloc(33);
if (!res)
return NULL;
res[32] = 0;
for (i = 31; i != -1; i--) {
res[i] = (n & 0x1) ? '1' : '0';
n >>= 1;
}
return res;
}
char *remove_lead_zeros(char *str)
{
char *s = str;
char *res = malloc(33);
if (!res)
return NULL;
while (*s == '0') {
s++;
}
strcpy(res, s);
free(str);
return res;
}
char *sum_bits(char *s1, char *s2)
{
int a = str_to_int(s1);
int b = str_to_int(s2);
int sum = a + b;
char *res;
if (!sum) {
res = malloc(2);
if (!res)
return NULL;
res[1] = 0;
res[0] = '0';
}
return remove_lead_zeros(int_to_bin(a + b));
}
int main()
{
char *s = sum_bits("111", "1");
if (!s)
return 1;
printf("%s\n", s);
free(s);
return 0;
}
// take 2 binary
- Anonymous February 29, 2016var addIt = function(a, b) {
var aI = parseInt(a, 2);
var bI = parseInt(b, 2);
var t = aI + bI;
// convert to binary
t = (t >>> 0).toString(2);
console.log(t);
};
addIt('111', '11');