## How does condition statement work with bit-wise operators?

I tried to understand how if condition work with bitwise operators. A way to check if a number is even or odd can be done by:

#include <iostream> #include <string> using namespace std; string test() { int i = 8; //a number if(i & 1) return "odd"; else return "even"; } int main () { cout << test(); return 0; }

The Part I don't understand is how the if condition work. In this case if i = 8 then the in If statement it is doing `1000 & 1`

which should gives back 1000 which equal 8.

If i = 7, then in if statement it should be doing `111 & 1`

which gives back 111 which equal 7

Why is it the case that if(8) will return "even" and if(7) return "odd"? I guess I want to understand what the if statement is checking to be True and what to be False when dealing with bit-wise operators.

Just A thought when I wrote this question down is it because it's actually doing

for 8: 1000 & 0001 which gives 0 for 7: 0111 & 0001 which gives 1?

Yes, you are right in the last part. Binary `&`

and `|`

are performed bit by bit. Since

1 & 1 == 1 1 & 0 == 0 0 & 1 == 0 0 & 0 == 0

we can see that:

8 & 1 == 1000 & 0001 == 0000

and

7 & 1 == 0111 & 0001 == 0001

Your `test`

function does correctly compute whether a number is even or odd though, because `a & 1`

tests whether there is a `1`

in the 1s place, which there only is for odd numbers.

**When is it appropriate to use a bitwise operator in a conditional ,** it is appropriate when you are performing a masking operation. if ((a & b) > 0) { } where a and b are integers. || and | and && and & are not� These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits. Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).

Actually, in C, C++ and other major programming languages the `&`

operator do `AND`

operations in each bit for integral types. The nth bit in a bitwise `AND`

is equal to 1 if and only if the nth bit of both operands are equal to 1.

For example:

8 & 1 = 1000 - 8 0001 - 1 ---- 0000 - 0 7 & 1 = 0111 - 7 0001 - 1 ---- 0001 - 1 7 & 5 = 0111 - 7 0101 - 5 ---- 0101 - 5

For this reason doing a bitwise `AND`

between an even number and `1`

will always be equal `0`

because only odd numbers have their least significant bit equal to `1`

.

**Operators,** These operators are used in conditional statements for if, while, and do-while To understand how the bitwise operators work on the bits on variable values you � The logical AND and logical OR operators both take two operands. Each operand is a boolean expression (i.e., it evaluates to either true or false). The logical AND condition returns true if both operands are true, otherwise, it returns false. The logical OR condition returns false if both operands are false, otherwise, it returns true.

`if(x)`

in C++ converts `x`

to boolean. An integer is considered `true`

iff it is nonzero.

Thus, all `if(i & 1)`

is doing is checking to see if the least-significant bit is set in `i`

. If it is set, `i&1`

will be nonzero; if it is not set, `i&1`

will be zero.

The least significant bit is set in an integer iff that integer is odd, so thus `i&1`

is nonzero iff `i`

is odd.

**C Bitwise Operators: AND, OR, XOR, Shift & Complement (with ,** Bitwise operators are used for manipulating a data at the bit level, also The bitwise logical operators work on the data bit by bit, starting from the least The result of the bitwise OR operation is 1 if at least one of the expression has the 7 ) C Conditional Statement � 8) C Loops � 9) Switch Case Statement� Bitwise complement operator ~ Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~. 35 = 00100011 (In Binary) Bitwise complement Operation of 35 ~ 00100011 _____ 11011100 = 220 (In decimal) Twist in bitwise complement operator in C Programming

The expression `i & 1`

, where `i`

is an `int`

, has type `int`

. Its value is `1`

or `0`

, depending on the value of the low bit of `i`

. In the statement `if(i & 1)`

, the result of that expression is converted to `bool`

, following the usual rule for integer types: `0`

becomes `false`

and non-zero becomes `true`

.

**The Bitwise Operators,** The bitwise operators are similar to the logical operators, except that they work on a smaller scale -- binary representations of data. The following operators are� The bitwise AND operator is a single ampersand: &. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND does the logical AND (as shown in the table above) of the bits in each position of a number in its binary form.

What you say the code is doing is actually how bit-wise operators are supposed to work. In your example of (8 & 1):

1000 & 0001 = 0000

because in the first value, the last bit is set to 0, while in the second value, the last bit is set to 1. 0 & 1 = 0.

0111 & 0001 = 0001

In both values, the last bit is set to 1, so the result is 1 since 1 & 1 = 1.

**Bit wise operators in C | C Operators and Expressions,** Bit wise operators in C:These operators are used to perform bit operations. C - Variable � C - Operators and Expressions � C - Decision Control statement into binary values which are the sequence of bits and bit wise operators work on these Conditional operators return one value if condition is true and returns another� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

**Logical vs. Bitwise Operators,** A logical operator is very similar to a bitwise operator in that it only checking the first condition, the second condition is ignored. Well first of all, logical operators only work when the result from each expression is a scalar. Study C MCQ Questions and Answers on C Bitwise Operators. Bitwise operators deal with ones and zeroes. Easily attend technical job interviews with these Multiple Choice Questions. Go through C Theory Notes on Bitwise Operators before studying these questions.

**Conditional operator,** The major difference is that bitwise operations operate on the individual bits of a binary numeral, whereas conditional operators operate on logical operations. Additionally, expressions before and after a bitwise operator will always be evaluated. If expression 1 is true, expression 2 and 3 will NOT be checked. In C, the following 6 operators are bitwise operators (work at bit-level) The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

**bitwise IF statement,** How does an IF statement react on bitwise commands like this: Is the IF statement TRUE or FALSE? Tnx the result of the & operation is 0x2 Converting this to Java is a trivial task what have you tried? It seems like you have lots of questions surrounding Bitwise statement I suggest you do some research on how they work then actually accept soem of your question's answers. This seems like a simple odd/even check based on who went first. – Security Hound Aug 27 '12 at 13:39