• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

XOR  RSS feed

 
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.
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!