C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Yes / No Questions (Q.No. 6)
6.
Bitwise can be used to perform addition and subtraction.
Discussion:
6 comments Page 1 of 1.
Saumya said:
8 years ago
The Answer should be A. Because we can perform addition as well as subtraction using bitwise XOR.
Pranali said:
8 years ago
Please explain the answer in detail.
David said:
1 decade ago
Whoever wrote this should swot up of course you can do bitwise addition and subtraction.
unsigned int myAdd(unsigned int a, unsigned int b)
{
unsigned int carry = a & b;
unsigned int result = a ^ b;
while(carry != 0)
{
unsigned int shiftedcarry = carry << 1;
carry = result & shiftedcarry;
result ^= shiftedcarry;
}
return result;
}
unsigned int myAdd(unsigned int a, unsigned int b)
{
unsigned int carry = a & b;
unsigned int result = a ^ b;
while(carry != 0)
{
unsigned int shiftedcarry = carry << 1;
carry = result & shiftedcarry;
result ^= shiftedcarry;
}
return result;
}
Atul said:
1 decade ago
A big circuit inside the computer is made up of adder and subtracters which can perform all the operations like addition, multiplication, subtraction and division so how it can't be performed Dmitri has given good example above.
Dmitri said:
1 decade ago
If bitwise operators can't be used for addition or subtraction, why can the hardware do it? It uses boolean operations on individual bits...
Also try the following function (which does addition with bitwise operators):
int bitadd(int x, int y)
{
int z = 0, c;
do {
z = x ^ y;
c = (x & y) << 1;
x = z;
y = c;
} while (c);
return z;
}
Also try the following function (which does addition with bitwise operators):
int bitadd(int x, int y)
{
int z = 0, c;
do {
z = x ^ y;
c = (x & y) << 1;
x = z;
y = c;
} while (c);
return z;
}
Cheran said:
1 decade ago
Bitwise operators can be used for multiplication and division only.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers