Can anyone explain the below code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Can anyone explain the below code

int main() { bool x=1,y=0; cout << ++x << ~~y; } output -- 10 what is the ~~ operator??

25th May 2017, 3:53 AM
MUʜAMMED ɪʟʟYAS
MUʜAMMED ɪʟʟYAS - avatar
13 Answers
+ 7
~~ is not an operator ~ (tilde) is a unary operator. It is the bitwise not. What does it do? It turns all the binary 1s to 0s and binary 0s to 1s. So, by using it twice, you are cancelling out the effect of both. So, 0 remains 0.
25th May 2017, 7:33 AM
Pixie
Pixie - avatar
+ 9
Hey! That's one of my quiz factory questions (Not yet approved)Where did you find that?😮
25th May 2017, 4:28 AM
Pixie
Pixie - avatar
+ 7
@Pixie same place...when I rate the quiz submissions I saw this one...and I stuck there.. So u can tell me better than anyone...how did u get the output 10??
25th May 2017, 4:45 AM
MUʜAMMED ɪʟʟYAS
MUʜAMMED ɪʟʟYAS - avatar
+ 6
but when x=0,it will increment and give the output 10 again..
26th May 2017, 1:43 AM
MUʜAMMED ɪʟʟYAS
MUʜAMMED ɪʟʟYAS - avatar
+ 6
@Chintu Verma why??
26th May 2017, 6:22 AM
MUʜAMMED ɪʟʟYAS
MUʜAMMED ɪʟʟYAS - avatar
+ 4
PS: Good on you for rating submissions. Not many people do it here. But those who do are the heroes this community deserves 😃
25th May 2017, 7:42 AM
Pixie
Pixie - avatar
+ 4
thnxx...🤗🤗🤗
25th May 2017, 7:44 AM
MUʜAMMED ɪʟʟYAS
MUʜAMMED ɪʟʟYAS - avatar
+ 2
As Venkatesh said, it is the "bitwise not" operator, meaning it reverses 0s and 1s. Assume byte y = 0x00100100; therefore ~y == 0x11011011; reversing it again gets you back to the original value.
25th May 2017, 4:21 AM
Jesse Bayliss
Jesse Bayliss - avatar
+ 1
it is the bitwise not two times. try with a type other than book, say int and you may get different result.
25th May 2017, 4:02 AM
Venkatesh Pitta
Venkatesh Pitta - avatar
+ 1
The bool values only output 0 or 1 when using cout. There are no spaces output when using cout in this example.
25th May 2017, 5:13 AM
Jesse Bayliss
Jesse Bayliss - avatar
+ 1
looks like the ++ operator did not increment the bool x, and the double ~ either returned the bool y to 0 (false) or did not change it. the output is a concatenation of values xy, or 10
25th May 2017, 8:45 PM
josue
+ 1
this code is wrong
26th May 2017, 6:21 AM
Prateek Verma
Prateek Verma - avatar
+ 1
!!
26th May 2017, 6:40 AM
durgesh majeti
durgesh majeti - avatar