Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Binary XOR and Strings size  RSS feed

 
Antoine Compagnie
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening,

I lost a contest today, it was about binary XOR. I had to do it in ten minutes.

INPUT:
2 binary numbers n1 and n2, separated by spaces.

OUTPUT:
The result of an OR between n1 and n2.

CONSTRAINTS:
n1 and n2 have the same number of digits.

EXAMPLE:
Input
001 011
Output
011


I first tried to access the length of these Strings to make a to by two comparison but I wasn't able to do it...
Furthermore tis code was too long compared to one having said having done it in 80 characters...



Can you help me learning

  • How to access strings size
  • How to do a binary XOR


  • Thanks in advance.
     
    Carey Brown
    Bartender
    Posts: 2980
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First off, you can find the documentation for String at http://docs.oracle.com/javase/8/docs/api/java/lang/String.html. In there you will find a method that will tell you the length of a String as well as how to pluck out an individual character.

    Concatenate (i.e. paste together) one String on to the end of another you can use '+', as in
    s3 = s3 + "abc"

    This will not work. Strings are immutable, meaning that once created you can't change them. You'll have to use concatenation instead. A proper program would use a StringBuilder but I'm guessing you're not ready for that yet.
    n3.charAt(i)=0;

    This will not work. You are using a variable 'i' when you have not yet declared it. "i=" is not a complete expression. "i--" is not a boolean expression. "i=0" will reset 'i' so it negates anything else you've done with 'i'.
    for(i=;i--;i=0)

    This will not work. "n1.charAt(i)=0" is not a boolean expression. I'm guessing you meant to use "==". You shouldn't be comparing a character to an integer, you probably want '0' (with the single quotes).
    if (n1.charAt(i)=0 && (n2.charAt(i)=0)



     
    Carey Brown
    Bartender
    Posts: 2980
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And there's this about XOR http://cplus.about.com/od/glossar1/g/xor.htm
     
    Liutauras Vilda
    Marshal
    Posts: 4631
    316
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is still not clear, what you had to do: XOR (what's in subject), OR (what's in quote)? These two are different.

    Antoine Compagnie wrote:Can you help me learning

    How to access strings size
    How to do a binary XOR

    Everything what you need to know from your two listed points, is the truth table of one or another operator (better to know both). To solve this exercise, you don't need computer. You need to come up with logical program's approach.

    Forget computer. You don't need to know how to access String size, it doesn't have size, it has length, anyway, what would you do with it? Try to solve it on a piece of paper. How you'd solve it by describing steps in english or other preferred language?

    This is what you'd get in both cases.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!