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
what is the function of pragma directive in c?
Explain how can I remove the trailing spaces from a string?
What is scanf () in c?
What is the scope of local variable in c?
What are the two types of structure?
What is the difference between single charater constant and string constant?
What is the difference between the = symbol and == symbol?
Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..
Is c dynamically typed?
a character or group of characters that defines a register,or a part of storage a) memory b) byte c) address d) linear list
What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.
how can f be used for both float and double arguments in printf? Are not they different types?
What are compound statements?
What is realloc in c?
Write the syntax and purpose of a switch statement in C.