首页 > 编程语言 > 详细

java学习笔记3——异或

时间:2017-01-22 07:59:20      阅读:242      评论:0      收藏:0      [点我收藏+]
异或原理:
  转换两个字符或数为2进制的ASCII码,再按位异或,即
    0001  0001  --->  0000
    0000  0000  --->  0000
    0001  0000  --->  0001
    0000  0001  --->  0001
异或交换位置例子
  import java.util.Scanner;
  public class xor {          //异或,英文为exclusive OR,或缩写成xor

    public static void main (String[] args){
      System.out.println("请输入x(enter) y(enter)");
      Scanner in = new Scanner(System.in);
      int x = in.nextInt();
      int y = in.nextInt();
      x = x^y;
      y = y^x;
      x = x^y;
      System.out.println("x=" +x);
      System.out.println("y=" +y);
    }
  }


例如输入5(0101) 9(1001)时,

    x = 0101;  y = 1001;

    x = x^y = 1100;

    y = y^x = 1001^1100 = 0101;

    x = x^y = 0101^1100 = 1001;

java学习笔记3——异或

原文:http://www.cnblogs.com/Cachiu/p/6339035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!