• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find XOR value of charcters of a charcter Array in java?  RSS feed

 
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There

What I am trying is find the XOR value between charcters of a String here is my code


but I am not getting actual XOR value of two charcters that I am trying to get

Thanks
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you expecting - what is the "actual XOR value of two characters" according to you? Can you give an example - if you have the characters 'A' and 'B', then what should be the XOR value according to you?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@jasper

Thanks for reply

lets take an example I have a string "10011".

first I want to calculate XOR value of char '1' and '0' and then I want to calculate XOR value of '0' and '0' and then '0' and '1' and finally '1' and '1'.All these charcters are of a Single String

Basically I want a Gray code of a binary(0s and 1s) string

Gray code http://en.wikipedia.org/wiki/Gray_code

I hope it is now clear
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want the XOR of those two chars or do you want the XOR value of their numerical representations?
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell

XOR value of these charcters not their numerical repersentations.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is '1' XOR '0' according to you?
If I were to take you literally I would say it is SOH.

The code above prints 1. But the result of code above is not a char '1'. In is an integer 1 which corresponds to SOH as (char) 1 .
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pawel


Thanks for giving SOH information.

So how to I acheive XOR operation in charcters '0' and '1'?

Do you any suggestions for this?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change char to integer (char '1' to integer 1), calculate XOR, change back to char.

Note that:won't give you the result you expect.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Pawel


Change char to integer (char '1' to integer 1), calculate XOR, change back to char.


COOL idea..

I have changed my code to this


Working as expecting

Thanks @pawel
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!