I want to write C or C++ functions that evaluate to ONE when the given conditions are true, and to ZERO when they are false. The following are the fourt conditions: int a(int x); //Any bit of x equals 0. int b(int x); // Any bit of x equals 1. int c(int x); //Any bit in the LSB of x equals 0. int d(int x); //Any bit in the MSB of x equals 1. The code should follow the bit-level integer coding rules, with the additional restriction that you may not use equality (==) or inequality (!=) tests.
I want to write C or C++ functions that evaluate to ONE when the given conditions are true, and to ZERO when they are false. The following are the fourt conditions:
int a(int x); //Any bit of x equals 0.
int b(int x); // Any bit of x equals 1.
int c(int x); //Any bit in the LSB of x equals 0.
int d(int x); //Any bit in the MSB of x equals 1.
The code should follow the bit-level integer coding rules, with the additional restriction that you may not use equality (==) or inequality (!=) tests.
To create C or C++ functions that evaluate to ONE when the given conditions are true and to ZERO when they are false, without using equality (==) or inequality (!=) tests, you can use bitwise operations. You can use these operations to manipulate and check individual bits within the integer x
. Here are the four functions a
, b
, c
, and d
along with explanations:
Step by step
Solved in 4 steps