Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(false - NOT (0)) is equal to 1?

Tags:

c++

A test question: What is the result of (false - ~0)

Why (false - ~0) is 1? On 32 bit machine ~0 is 11....11 where there are 32 1 bits right? Then false to int conversion is 00....00 also 32 times, right? So we subtract from 32 0 32 1? Does it underflow and we get 1?

like image 923
Narek Avatar asked Dec 01 '22 15:12

Narek


1 Answers

0 is int~0 is int and equal -1false gets promoted to int, which results in zero → your expression is calculated as (0 - (-1)) which equals 1.

like image 61
CiaPan Avatar answered Dec 19 '22 16:12

CiaPan