今天在写程序的时候,突然想起来以前学数字电路的异或运算,同或运算。而在java语言中只找到了异或运算(^),却没有同或运算。
1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0
^ 同或运算
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
= 1 0 1 0 0 0 1 1 = 0 1 0 1 1 1 0 0
异或运算 : 相同为0,不同为1;
同或运算: 相同为1,不同为0;
总结规律:同或运算 = 异或运算 ^ 1
(A ^ B) ^ 1
程序如下:
import java.util.Scanner; public class problem7 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); String strA = sc.nextLine(); String strB = sc.nextLine(); byte[] byteA = strA.getBytes(); byte[] byteB = strB.getBytes(); for(int i = 0;i