Embedded C Interview Questions with Answers | Part 2
How to Add Two Numbers without using any arithmetic operators in C
If you know the Half duplex & Full duplex circuit then probably it is easy for you to understand this problem.
In Duplex circuit, you will use two gates AND & XOR.
Here also you need to do the bitwise AND & XOR operation for each bit for the variable. Generated carry for the first bit will be transferred for next bit calculation.
To Make it easy I will show you the basic example:
0+0: Sum = 0
1+0 : Sum =1
0+1 : Sum = 1
1+1 : Sum = 10 (2)
But in a single bit we can not accommodate 10 = 2. So we need a carry bit.
Which will hold this second bit?
So now,
1+1 : Sum = 0 with Carry = 1
So now,
0+0: Sum = 0 with carry = 0
1+0 : Sum =1 with carry = 0
0+1 : Sum = 1 with carry =0
1+1 : Sum = 0 with carry =1
If you see the output observed for carry bit is equal to the truth table of AND gate.
In Out
(0,0) = 0
(0,1) = 0
(1,0) = 0
(1,1) = 1
And output observed for sum bit is equal to the truth table of XOR gate.
But this is true for one-bit calculation, what happen if we have 8 bits means we have the byte of data for addition.
Example:1
2-bit addition
11 = 3
+ 10 = 2
_____
101
First Bit addition: 0+1, sum =1, carry = 0, now this carry will be passed for second bit operation.
Second bit addition: 1+1+0, sum = 0, carry = 1
Example:2
2-bit addition
11 = 3
+ 11 = 3
_____
110 = 6
First Bit addition: 1+1, sum =0, carry = 1, now this carry will be passed for second bit operation.
Second bit addition: 1+1+1, sum = 1, carry = 1
or
(1+1, sum = 0 carry = 1 then 0+1, sum = 1)
Example:3
2-bit addition
11 = 3
+ 01 = 1
_____
100 = 4 (1= carry, 0=sum, 0=sum)
First Bit addition: 1+1, sum =0, carry = 1, now this carry will be passed for second bit operation.
Second bit addition: 1+1+0, sum = 0, carry = 1
or
(1+1, sum = 0 carry = 1 then 0+1, sum = 1)
Once carry bit is 1 then it will remain as 1 then we need to add it with next bit sum to generate the new carry.
Below code show the implementation:
#include<stdio.h>
#include <stdlib.h>
void printbin(int pnum){
if(pnum != 0)
{
printbin(pnum >> 1);
if (pnum & 1){
printf("1");
}
else{
printf("0");
}
}
else{
printf("0");
}
}
#define VNAME(x) #x
#define F_PRINTBIN(pnum) { \
printf("%s :",VNAME(pnum)); \
printbin(pnum); \
printf(",%d",pnum); \
printf("\n"); \
}
void _printbin(int pnum){
printbin(pnum);
printf("\n");
}
int Sum(int x, int y)
{
int carry, sum, correctSum, newCarry;
correctSum = x + y;
sum = x;
F_PRINTBIN(x);
F_PRINTBIN(y);
F_PRINTBIN(correctSum);
while (y != 0)
{
carry = sum & y;
sum = sum ^ y;
F_PRINTBIN(carry);
y = carry << 1;
F_PRINTBIN(sum);
F_PRINTBIN(y);
}
return sum;
}
int main()
{
int a = 10;
int b = 15;
printf("Final sum = %d\n", Sum(a, b));
return 0;
}
Output:
x :01010,10
y :01111,15
correctSum :011001,25
carry :01010,10
sum :0101,5
y :010100,20
carry :0100,4
sum :010001,17
y :01000,8
carry :0,0
sum :011001,25
y :0,0
Final sum = 25
In Duplex circuit, you will use two gates AND & XOR.
Here also you need to do the bitwise AND & XOR operation for each bit for the variable. Generated carry for the first bit will be transferred for next bit calculation.
To Make it easy I will show you the basic example:
0+0: Sum = 0
1+0 : Sum =1
0+1 : Sum = 1
1+1 : Sum = 10 (2)
But in a single bit we can not accommodate 10 = 2. So we need a carry bit.
Which will hold this second bit?
So now,
1+1 : Sum = 0 with Carry = 1
So now,
0+0: Sum = 0 with carry = 0
1+0 : Sum =1 with carry = 0
0+1 : Sum = 1 with carry =0
1+1 : Sum = 0 with carry =1
If you see the output observed for carry bit is equal to the truth table of AND gate.
In Out
(0,0) = 0
(0,1) = 0
(1,0) = 0
(1,1) = 1
And output observed for sum bit is equal to the truth table of XOR gate.
But this is true for one-bit calculation, what happen if we have 8 bits means we have the byte of data for addition.
Example:1
2-bit addition
11 = 3
+ 10 = 2
_____
101
First Bit addition: 0+1, sum =1, carry = 0, now this carry will be passed for second bit operation.
Second bit addition: 1+1+0, sum = 0, carry = 1
Example:2
2-bit addition
11 = 3
+ 11 = 3
_____
110 = 6
First Bit addition: 1+1, sum =0, carry = 1, now this carry will be passed for second bit operation.
Second bit addition: 1+1+1, sum = 1, carry = 1
or
(1+1, sum = 0 carry = 1 then 0+1, sum = 1)
Example:3
2-bit addition
11 = 3
+ 01 = 1
_____
100 = 4 (1= carry, 0=sum, 0=sum)
First Bit addition: 1+1, sum =0, carry = 1, now this carry will be passed for second bit operation.
Second bit addition: 1+1+0, sum = 0, carry = 1
or
(1+1, sum = 0 carry = 1 then 0+1, sum = 1)
Once carry bit is 1 then it will remain as 1 then we need to add it with next bit sum to generate the new carry.
Below code show the implementation:
#include<stdio.h>
#include <stdlib.h>
void printbin(int pnum){
if(pnum != 0)
{
printbin(pnum >> 1);
if (pnum & 1){
printf("1");
}
else{
printf("0");
}
}
else{
printf("0");
}
}
#define VNAME(x) #x
#define F_PRINTBIN(pnum) { \
printf("%s :",VNAME(pnum)); \
printbin(pnum); \
printf(",%d",pnum); \
printf("\n"); \
}
void _printbin(int pnum){
printbin(pnum);
printf("\n");
}
int Sum(int x, int y)
{
int carry, sum, correctSum, newCarry;
correctSum = x + y;
sum = x;
F_PRINTBIN(x);
F_PRINTBIN(y);
F_PRINTBIN(correctSum);
while (y != 0)
{
carry = sum & y;
sum = sum ^ y;
F_PRINTBIN(carry);
y = carry << 1;
F_PRINTBIN(sum);
F_PRINTBIN(y);
}
return sum;
}
int main()
{
int a = 10;
int b = 15;
printf("Final sum = %d\n", Sum(a, b));
return 0;
}
Output:
x :01010,10
y :01111,15
correctSum :011001,25
carry :01010,10
sum :0101,5
y :010100,20
carry :0100,4
sum :010001,17
y :01000,8
carry :0,0
sum :011001,25
y :0,0
Final sum = 25
How to Add Two int Numbers Using printf Function in C
Program:
int addition(int x, int y)
{
return printf("%*c%*c",x,'o',y,'h');
}
void main()
{
printf("Sum = \n%d\n", addition(5, 7));
}
Output: o hSum = 12
Here in this program we can see that we have written "%*c". Here * is used to pass the variable length to display for the given character. So here in addition function we have passed the argument as 5 and 7. It will act as %5C %7C in the printf function.
As we all know that printf will return the number of character print successfully. So at the end it will print a total number of character which is equal to 5+7= 12.
Here There is 5 character is given to print "o" & 7 character is reserved to print h. So you can see the space between both characters.
Now if you change the number passed in argument to the function addition with 1 & 2 then you can see the difference in output.
int addition(int x, int y)
{
return printf("%*c%*c",x,'o',y,'h');
}
void main()
{
printf("Sum = %d\n", addition(1, 2));
}
int addition(int x, int y)
{
return printf("%*c%*c",x,'o',y,'h');
}
void main()
{
printf("Sum = \n%d\n", addition(5, 7));
}
Output: o hSum = 12
Here in this program we can see that we have written "%*c". Here * is used to pass the variable length to display for the given character. So here in addition function we have passed the argument as 5 and 7. It will act as %5C %7C in the printf function.
As we all know that printf will return the number of character print successfully. So at the end it will print a total number of character which is equal to 5+7= 12.
Here There is 5 character is given to print "o" & 7 character is reserved to print h. So you can see the space between both characters.
Now if you change the number passed in argument to the function addition with 1 & 2 then you can see the difference in output.
int addition(int x, int y)
{
return printf("%*c%*c",x,'o',y,'h');
}
void main()
{
printf("Sum = %d\n", addition(1, 2));
}
Output:
o hSum = 3
Here in the output you can see only character is used to print the "o" and 2 characters is used for printing "h" in the output as we have specified 1 & 2 in the argument to addition function.
Find Larger Value of Two Numbers Without Using Any Relational Operators in C
#include<stdio.h>
#define SIGNOF(i) ((unsigned)(i) >> (sizeof (int) * 8 - 1))
#define CHECKMSB(type,a,b) ((a-b) & ((1 << (sizeof(type) * 8) - 1)))
int max(int a, int b)
{
int p[2] = {a,b};
return p[SIGNOF(a - b)];
}
int max_bymsb(int a, int b)
{
return CHECKMSB(int,a,b) ? b:a;
}
void main()
{
int a = 55,b = 30;
int x=max(a,b);
printf("Maximum number : %d\n",x);
printf("a-b : %x\n",(a-b));
printf("1 shift 31 times : %x\n",1 << 31);
printf("sizeof int : %d\n",(((sizeof(int) * 8)- 1)));
printf("Flag : %x\n",CHECKMSB(int,a,b));
printf("Max : %d\n",max_bymsb(a,b));
}
#define SIGNOF(i) ((unsigned)(i) >> (sizeof (int) * 8 - 1))
#define CHECKMSB(type,a,b) ((a-b) & ((1 << (sizeof(type) * 8) - 1)))
int max(int a, int b)
{
int p[2] = {a,b};
return p[SIGNOF(a - b)];
}
int max_bymsb(int a, int b)
{
return CHECKMSB(int,a,b) ? b:a;
}
void main()
{
int a = 55,b = 30;
int x=max(a,b);
printf("Maximum number : %d\n",x);
printf("a-b : %x\n",(a-b));
printf("1 shift 31 times : %x\n",1 << 31);
printf("sizeof int : %d\n",(((sizeof(int) * 8)- 1)));
printf("Flag : %x\n",CHECKMSB(int,a,b));
printf("Max : %d\n",max_bymsb(a,b));
}
Output:
Maximum number: 55
Maximum number: 55
a-b : 19
1 shift 31 times : 80000000
sizeof int : 31
Flag : 0
Max : 55
Here Program you can see the two different Macro is used. Both is used to check the sign or MSB of the number.
But Why we need to check the MSB or Sign?
Logic is very simple. We can judge the bigger number based on sign value.
1)
If we subtract the bigger value from small value then we will get the negative number means sign is 1.
1 shift 31 times : 80000000
sizeof int : 31
Flag : 0
Max : 55
Here Program you can see the two different Macro is used. Both is used to check the sign or MSB of the number.
But Why we need to check the MSB or Sign?
Logic is very simple. We can judge the bigger number based on sign value.
1)
If we subtract the bigger value from small value then we will get the negative number means sign is 1.
2)
But If subtract smaller value from bigger value then we will get positive number means sign is 0.
We know what we are subtracting and from what. So based on that we can get the smaller and bigger value.
If you feel there can be a scope of improvement in this then please suggest.
Post a Comment