Bit Manipulation Bit basics

104. Odd or Even with Bits

Easy

Given an integer n, return true if n is odd and false if it is even.

A number is odd when it is not a multiple of 2. Negative numbers follow the same rule, so −7 is odd, and −4 and 0 are even.

n is stored as a 64-bit signed integer. A negative number is stored in two's complement: −x is the bits of x flipped, plus 1.

Example 1

Input: n = 37

Output: true

Explanation: 37 = 2 × 18 + 1, so 37 is not a multiple of 2.

Example 2

Input: n = -9

Output: true

Explanation: −9 = 2 × (−5) + 1. It is not a multiple of 2, so it is odd, like 9.

Example 3

Input: n = -1000000000000

Output: false

Explanation: −1012 = 2 × (−5 × 1011), so it is even. n can go far past what 32 bits hold.

Check your understanding

Work out the answer for this input before you write any code.

n = -15

Constraints

  • -(253 − 1) ≤ n ≤ 253 − 1

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.