without using arithmatic operator convert an intger variable
x into x+1

Answer Posted / vadivel t

Hi,

In addition to the Answer#2, which i have posted already,
here i am posting another program which can be used to add
any two nos without using arithmatic operators.

Note: The program written below follows a logic of binary
addition.

#include<stdio.h>
#include<conio.h>

void main()
{
int no1, no2, size, i;
int res = 0, carry = 0, temp1 = 0, temp2 = 0;

size = sizeof(int) * 8;

printf("ENTER THE TWO NOS TO BE ADDED: \n");
scanf("%d %d", &no1, &no2);

for(i = 0; i < size ; i++)
{
temp1 = (no1 & 0x01 << i) ? 1 : 0;
temp2 = (no2 & 0x01 << i) ? 1 : 0;

if((temp1 & temp2) == 1 && (carry == 1))
{
res = res | 0x01 << i;
carry = 1;
}
else if((temp1 & temp2) == 1 && (carry == 0))
{
res = res | 0x00;
carry = 1;
}
else if((temp1 | temp2) == 1 && (carry == 1))
{
res = res | 0x00;
carry = 1;
}
else if((temp1 | temp2) == 1 && (carry == 0))
{
res = res | 0x01 << i;
carry = 0;
}
else if((temp1 | temp2) == 0 && (carry == 1))
{
res = res | 0x01 << i;
carry = 0;
}
else if((temp1 | temp2) == 0 && (carry == 0))
{
res = res | 0x00;
carry = 0;
}
else
{
/*Fatal Error*/
}
}
printf("\nRESULT: %d", res);
_getch();
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is the function of pragma directive in c?

632


Explain how can I remove the trailing spaces from a string?

632


What is scanf () in c?

667


What is the scope of local variable in c?

582


What are the two types of structure?

581






What is the difference between single charater constant and string constant?

627


What is the difference between the = symbol and == symbol?

631


Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..

651


Is c dynamically typed?

672


a character or group of characters that defines a register,or a part of storage a) memory b) byte c) address d) linear list

634


What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.

1788


how can f be used for both float and double arguments in printf? Are not they different types?

612


What are compound statements?

633


What is realloc in c?

582


Write the syntax and purpose of a switch statement in C.

631