Get the latest tech news

Test if a number is even


Problem You want to test if a number is even. Solution Well, the well-known solution is to test the value of n modulo 2. If it’s zero, then the number is even, otherwise it’s odd. But h…

Well, the well-known solution is to test the value of n modulo 2. You can use the bitwise AND operator and check the least significant bit of the integer. I think the optimizer recognizes modulo 2 and converts it to bitwise AND.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of number

number

Related news:

News photo

Chinese social media app RedNote is the number one app as TikTok ban looms

News photo

Telegram Hands U.S. Authorities Data on Thousands of Users. The number of data requests fulfilled by Telegram skyrocketed, with the company providing data to U.S. authorities on 2,253 users last year.

News photo

Number of US Venture Capital Firms Falls as Cash Flows To Tech's Top Investors