Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "n&1 == 0" always return false? [duplicate]

Tags:

c++

c

c++11

Why does the expression n&1 == 0 always return false, where n is an integer?

I want to use bitwise operation to determine whether n is even. However, it always return false. (The clion also prompted me that it always returns false).

What's more, it works when I use n&1 != 0 to determine whether n is odd.

like image 292
JunGor Avatar asked Apr 16 '16 02:04

JunGor


People also ask

Why do we write N2 instead of N?

Explanation: The chemical formula of Nitrogen is N but Nitrogen exists in a molecule of two ions hence the chemical symbol of Nitrogen gas is written as N2.

Is nitrogen N2 or just N?

Nitrogen (N2) Nitrogen is a chemical element that has the symbol N and atomic number 7 and atomic mass 14.00674µ. Elemental nitrogen is a colorless, odorless, tasteless and mostly inert diatomic gas at standard conditions, constituting 78% by volume of Earth's atmosphere.

Is N2 and N same?

N2 is nitrogen gas and there is chemical bond between two nitrogen atoms. 2N represents just two atoms of nitrogen that are not chemically bonded. Simply put, 2N are two atoms of nitrogen, while N2 is one molecule of nitrogen. Was this answer helpful?

What does N equal in math?

In mathematics, N is the symbol for natural numbers. N is represented as the set of natural numbers. N = 1,2,3,4,5,6,7,8,9,10,….. Learn more here: Mathematical Symbols.


1 Answers

Its because of the operator precedence.

== has higher precedence than the & operator, so 1 == 0 gets evaluated first to 0. Then the bit wise AND is performed which ultimately returns false.

like image 200
QuikProBroNa Avatar answered Oct 07 '22 13:10

QuikProBroNa