This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

XOR  RSS feed

 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a question.
1. byte b1 = -5;
2. int i = 0xff;
3. byte b2 = (byte)(b1 ^ i);
4. b2++;
5. System.out.println(b2);
ans is 5, How? Can anyone explain, please?
b1 will be 1111 1101, will get promoted to int before ^.
i will be 1111 1111.
After ^, 0000 0010. After that ?
Thanks.
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jini,
Let me point that -5 in binary is 1111 1011 and not 1111 1101 .
so we have

Now 0000 0100 in Binary is equal = 4 in decimal so incrementing
it we get "5". Hope this clears your querry.
Ravindra Mohan
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!