![]() |
VOOZH | about |
Given a number n, check whether it is even or odd. Return true for even and false for odd.
Examples:
Input: n = 15
Output: false
Explanation: 15 % 2 = 1, so 15 is odd .Input: n = 44
Output: true
Explanation: 44 % 2 = 0, so 44 is even.
Table of Content
We can check the remainder when divided by 2. If the remainder is 0, the number is even, otherwise it is odd. For example, when we divide 13 by 2, we get remainder as 1 and when we divide 14 by 2, we get remainder as 0.
false
The last bit of all odd numbers is always 1, while for even numbers itβs 0. So, when performing bitwise AND operation with 1, odd numbers give 1, and even numbers give 0.
Note: Bitwise operators are extremely fast and efficient because they operate directly at the binary level, making them significantly faster than arithmetic or logical operations.
Examples:
15 -> 1 1 1 1
& 0 0 0 1
-------
0 0 0 1 , so this we can say it is an odd number.44 -> 1 0 1 1 0 0
& 0 0 0 0 0 1
----------
0 0 0 0 0 0 , so this we can say it is an even number.
false