或运算:00010011 | 11010010
与运算:00010011 & 11010010

解决方案 »

  1.   

    首先要看你是什么格式的数据。如果你的数据是byte、int、long之类的数值型,比方说
    int a = 5; 
    int b = 7;
    那么你直接写a&b或者a|b这样的表达式就可以了。如果你是要对String类的对象进行操作,那就要自己写相应的方法来处理。接受2个String对象,返回一个String的结果。
      

  2.   

    例1:byte a = 5;           // 00000101 
    byte b = 2;           // 00000010
    byte c = (byte)(a&b); // 00000000
    byte d = (byte)(a|b); // 00000111例2:  public static String bitwiseOr(String a, String b, int size) {
        StringBuffer res = new StringBuffer(); 
        for (int i = 0; i < size; i++) {
          if (a.charAt(i)=='1'||b.charAt(i)=='1') res.append('1');
          else res.append('0');
        }
        return res.toString();
      }  public static String bitwiseAnd(String a, String b, int size) {
        StringBuffer res = new StringBuffer(); 
        for (int i = 0; i < size; i++) {
          if (a.charAt(i)=='1'&&b.charAt(i)=='1') res.append('1');
          else res.append('0');
        }
        return res.toString();
      }
      

  3.   

    注意:我举的例子代码为了图方便,需要调用者保证两个string一样长,并提供长度值。实际应用中应作适当改写。关于第一个例子,也是图方便,用了byte,这样后面的0/1序列说明就只需要8位长。后两个作了强制转换,因为&和|操作出来的应该是int,转换一下就只需要写8位了。我是不是很懒?:P