-
-
Save mariiaKolokolova/6c1df75d9bfadb0281d74d170c8b1578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maricka.kolokolova; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Input first number:"); | |
int a = sc.nextInt(); | |
System.out.println("Input second number:"); | |
int b = sc.nextInt(); | |
String str = Integer.toBinaryString(a ^ b); | |
String strA = Integer.toBinaryString(a); | |
String strB = Integer.toBinaryString(b); | |
System.out.println("Your binaries number:" + strA +" and "+ strB); | |
System.out.println("Hamming distance is: " + hemming(str)); | |
sc.close(); | |
} | |
static int hemming(String str) { | |
char[] mas = str.toCharArray(); | |
int hem = 0; | |
for (int i = 0; i < mas.length; i++) { | |
if (mas[i] == '1') { | |
hem++; | |
} | |
} | |
return hem; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment