What does the expression value & 1 yield?

Enhance your programming skills with the RECF Programming Test. Features flashcards and multiple choice questions with hints and explanations. Prepare for success!

Multiple Choice

What does the expression value & 1 yield?

Explanation:
Bitwise AND with 1 isolates the least significant bit. The number 1 has a binary form with a 1 only in the least significant position, so ANDing any value with 1 keeps that bottom bit and clears all higher bits to 0. The result is either 0 or 1: 0 if the value is even, 1 if the value is odd. This makes value & 1 a quick check for oddness and a direct way to obtain the least significant bit. It’s not the parity of the entire number (which would involve counting all 1s), and it doesn’t divide by 2 or reveal the most significant bit.

Bitwise AND with 1 isolates the least significant bit. The number 1 has a binary form with a 1 only in the least significant position, so ANDing any value with 1 keeps that bottom bit and clears all higher bits to 0. The result is either 0 or 1: 0 if the value is even, 1 if the value is odd. This makes value & 1 a quick check for oddness and a direct way to obtain the least significant bit. It’s not the parity of the entire number (which would involve counting all 1s), and it doesn’t divide by 2 or reveal the most significant bit.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy