Ejemplo n.º 1
0
int main()
{
	char* a = "011";
	char* b = "1";
	char* c = addBinary(a, b);
	printf_s("The result is: %s\n", c);
	return 0;
}
Ejemplo n.º 2
0
 string addBinary(string a, string b) {
     if(a.size()==0){
         return b;
     }
     if(b.size()==0){
         return a;
     }
     if(a[a.size()-1] == '1' && b[b.size()-1] == '1'){
         return addBinary(addBinary(
             string(a.begin(), a.end()-1), string(b.begin(), b.end()-1)), "1") + "0";
     }
     if(a[a.size()-1] == '0' && b[b.size()-1] == '0'){
         return addBinary(string(a.begin(), a.end()-1), string(b.begin(), b.end()-1)) + "0";
     }
     else{
         return addBinary(string(a.begin(), a.end()-1), string(b.begin(), b.end()-1)) + "1";
     }
 }
Ejemplo n.º 3
0
 string addBinary(string a, string b) {
     if(a.size() < b.size()) return addBinary(b, a);
     int i = a.size()-1, j = b.size()-1, carry = 0;
     
     while(i>=0 && (j>=0 || carry)){
         carry += (a[i]-'0') + (j<0? 0 : b[j--]-'0');
         a[i--] = '0' + (carry&1);
         carry >>= 1;
     }
     return carry>0 ? '1'+a : a; 
 }
Ejemplo n.º 4
0
int main(int arg, char *argv[]) {
    // insert code here...
    printf("LeetCode 067 Add Binary, C ...\n\n");

    char a[] = "11000111110010001100000111110110100110011110100010010101";
    char b[] = "111101001101010101001010000100001111010100101111010011";

    char *r = addBinary(a, b);
    printf("%s\n", r);

    return 0;
}
Ejemplo n.º 5
0
int main(int argc, char **argv) 
{
	char *a = "111";
	char *b = "1101";
	char *result = NULL;

	result = addBinary(a, b);

	if(result) {
		printf("a + b = %s\n", result);
	}

	return 0;
}
Ejemplo n.º 6
0
 string addBinary(string a, string b) {
     if (a.length() < b.length())
         return addBinary(b, a);
     int n = b.length(), m = a.length();
     int carry = 0, i, j;
     for (i = n - 1, j = m - 1; i >= 0; --i, --j) {
         int ai = a[j] - '0', bi = b[i] - '0';
         a[j] = (ai ^ bi ^ carry) + '0';
         carry = ai + bi + carry >= 2;
     }
     n = a.length() - n;
     while (--n >= 0) {
         int temp = a[n] - '0';
         a[n] = (carry ^ temp) + '0';
         carry &= temp;
     }
     if (carry) {
         a = '1' + a;
     }
     return a;
 }
Ejemplo n.º 7
0
    string addBinary(string a, string b) {
        int a_len = a.length();
        int b_len = b.length();
        if (!a_len && !b_len)
        	return "0";
        if (a_len < b_len)
        	return addBinary(b, a);
        int i = a_len - 1;
        int j = b_len - 1;
        int flag = 0;
        while (i >= 0)
        {
        	if (j >= 0)
        	{
        		int temp = (a[i] - '0') + (b[j] - '0') + flag;
        		if (temp >= 2)
        			flag = 1;
        		else
        			flag = 0;
        		a[i] = (temp % 2) + '0';
        	}
        	else
        	{
        		int temp = (a[i] - '0') + flag;
        		if (temp >= 2)
        			flag = 1;
        		else
        			flag = 0;
        		a[i] = (temp % 2) + '0';
        	}

        	i --;
        	j --;
        }
        if (flag)
        	a.insert(a.begin(), '1');
        return a;
    }
Ejemplo n.º 8
0
bool OSSerialize::binarySerialize(const OSMetaClassBase *o)
{
    OSDictionary * dict;
    OSArray      * array;
    OSSet        * set;
    OSNumber     * num;
    OSSymbol     * sym;
    OSString     * str;
    OSData       * ldata;
    OSBoolean    * boo;

	unsigned int  tagIdx;
    uint32_t   i, key;
    size_t     len;
    bool       ok;

	tagIdx = tags->getNextIndexOfObject(o, 0);
	// does it exist?
	if (-1U != tagIdx)
	{
		key = (kOSSerializeObject | tagIdx);
		if (endCollection)
		{
			 endCollection = false;
			 key |= kOSSerializeEndCollecton;
		}
		ok = addBinary(&key, sizeof(key));
		return (ok);
	}

	if ((dict = OSDynamicCast(OSDictionary, o)))
	{
		key = (kOSSerializeDictionary | dict->count);
		ok = addBinaryObject(o, key, NULL, 0);
		for (i = 0; ok && (i < dict->count);)
		{
			const OSSymbol        * dictKey;
			const OSMetaClassBase * dictValue;
			const OSMetaClassBase * nvalue = 0;

			dictKey = dict->dictionary[i].key;
			dictValue = dict->dictionary[i].value;
			i++;
			if (editor)
			{
				dictValue = nvalue = (*editor)(editRef, this, dict, dictKey, dictValue);
				if (!dictValue) dictValue = dict;
			}
			ok = binarySerialize(dictKey);
			if (!ok) break;
			endCollection = (i == dict->count);
			ok = binarySerialize(dictValue);
			if (!ok) ok = dictValue->serialize(this);
			if (nvalue) nvalue->release();
//			if (!ok) ok = binarySerialize(kOSBooleanFalse);
		}			
	}
	else if ((array = OSDynamicCast(OSArray, o)))
	{
		key = (kOSSerializeArray | array->count);
		ok = addBinaryObject(o, key, NULL, 0);
		for (i = 0; ok && (i < array->count);)
		{
			i++;
			endCollection = (i == array->count);
			ok = binarySerialize(array->array[i-1]);
			if (!ok) ok = array->array[i-1]->serialize(this);
//			if (!ok) ok = binarySerialize(kOSBooleanFalse);
		}			
	}
	else if ((set = OSDynamicCast(OSSet, o)))
	{
		key = (kOSSerializeSet | set->members->count);
		ok = addBinaryObject(o, key, NULL, 0);
		for (i = 0; ok && (i < set->members->count);)
		{
			i++;
			endCollection = (i == set->members->count);
			ok = binarySerialize(set->members->array[i-1]);
			if (!ok) ok = set->members->array[i-1]->serialize(this);
//			if (!ok) ok = binarySerialize(kOSBooleanFalse);
		}			
	}
	else if ((num = OSDynamicCast(OSNumber, o)))
	{
		key = (kOSSerializeNumber | num->size);
		ok = addBinaryObject(o, key, &num->value, sizeof(num->value));
	}
	else if ((boo = OSDynamicCast(OSBoolean, o)))
	{
		key = (kOSSerializeBoolean | (kOSBooleanTrue == boo));
		ok = addBinaryObject(o, key, NULL, 0);
	}
	else if ((sym = OSDynamicCast(OSSymbol, o)))
	{
		len = (sym->getLength() + 1);
		key = (kOSSerializeSymbol | len);
		ok = addBinaryObject(o, key, sym->getCStringNoCopy(), len);
	}
	else if ((str = OSDynamicCast(OSString, o)))
	{
		len = (str->getLength() + 0);
		key = (kOSSerializeString | len);
		ok = addBinaryObject(o, key, str->getCStringNoCopy(), len);
	}
	else if ((ldata = OSDynamicCast(OSData, o)))
	{
		len = ldata->getLength();
		if (ldata->reserved && ldata->reserved->disableSerialization) len = 0;
		key = (kOSSerializeData | len);
		ok = addBinaryObject(o, key, ldata->getBytesNoCopy(), len);
	}
	else return (false);

    return (ok);
}
Ejemplo n.º 9
0
void testAddBinary(){
    string a = "1101";
    string b = "10";
    string res = addBinary(a, b);
    cout << res << endl;
}
Ejemplo n.º 10
0
int main(int argc, char *argv[]) {
    printf("%s\n", addBinary("11", "1"));
    return 0;
}
Ejemplo n.º 11
0
int main(){
    char* a ="101010010000110110111100101111000111111011001110111001111000";
    char* b ="10001110100001111000001110001000000101011011100001011101101001000000101001111010111001110000";
    printf("%s \n",addBinary(a,b));
    return 0;
}