奇校验

import java.util.Scanner;

public class test07 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            String s=scanner.nextLine();
            for(int i=0;i<s.length();i++){
                int ch=s.charAt(i);
                if(c(ch)%2==0){//若1的个数是偶数
                    ch+=128;

                }
                System.out.println(num(ch));
            }
        }
    }
    public static int c(int n){
        int count=0;
        int b=1;
        for(int i=0;i<8;i++){
            if((n&b)==b){
                count++;
            }
            b=(b<<1);
        }return count;
    }
    public static String num(int n){
        int b=128;
        StringBuilder string=new StringBuilder();
        for(int i=0;i<8;i++){
            if((n&b)==b){
                string.append("1");
            }else{
                string.append("0");
            }
            b=(b>>>1);
        }return string.toString();
    }
}

你可能感兴趣的:(Java)