In Java, what is the result of applying bitwise NOT to 5?

Prepare for the Computer Programming Test. Dive into a world of coding with multiple choice questions, flashcards, and detailed explanations. Boost your knowledge and be exam-ready!

Multiple Choice

In Java, what is the result of applying bitwise NOT to 5?

Explanation:
Bitwise NOT flips every bit in the value. In Java, an int is a 32-bit signed number using two’s complement representation. The value 5 is stored as 00000000 00000000 00000000 00000101. Flipping all bits gives 11111111 11111111 11111111 11111010, which, when interpreted as a signed 32-bit integer, equals -6. A quick check using the common identity for two’s complement integers, ~x = -x - 1, also gives ~5 = -5 - 1 = -6.

Bitwise NOT flips every bit in the value. In Java, an int is a 32-bit signed number using two’s complement representation. The value 5 is stored as 00000000 00000000 00000000 00000101. Flipping all bits gives 11111111 11111111 11111111 11111010, which, when interpreted as a signed 32-bit integer, equals -6. A quick check using the common identity for two’s complement integers, ~x = -x - 1, also gives ~5 = -5 - 1 = -6.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy