1.第三位是否为1 if((byte1 & 4) == 4){
                  //第三位是1。
  相同的第一位-第七位1~64,第八位直接判断byte1<或>0。
3.(byte1&-1)!=0作为判断条件。
4。通过移位比较来实现
2.不是很明白你的意思,应该方式差不多。

解决方案 »

  1.   

    下面是对int型和long类型的数据进行二进制输出。至于是byte类型和short的,将字节长度从32改为8和16即可。
    至于你需要的
    1、指定位是否为1,对转化后的字符串进行str.charAt(int dex)处理后和"1"进行对比即可;
    2、任意位是否为1,其实只要判断0是否存在即可,即str.indexOf("0")==-1是全部为1;
    3、与2相反
    4、值是1的个数,可以在转换函数中修改,比如将pBinInt中的循环if(((1 << j) &  i) != 0)时定一个整形的k++,最后写个int方法返回k即可。
    如果不想这么复杂,同样可以对结果str处理。使用str.indexOf("1")对其循环截取,每次循环k++直至str.indexOf("1")==-1即可。public class Test {
      public static void main(String[] args) {
        String str;
        String str2;
        int i = 30;
        long j = 1200378434;
        str = Test.pBinInt(i);
        System.out.println(str);
        str2 = Test.pBinLong(j);
        System.out.println(str2);
      }static String pBinInt(int i) {
        String stri = "";
        for(int j = 31; j >=0; j--) {
          if(((1 << j) &  i) != 0)
            stri = stri+"1";
          else
            stri = stri+"0";
        }
        return stri;
      }
      static String pBinLong(long l) {
        String strl = "";
        for(int i = 63; i >=0; i--) {
          if(((1L << i) & l) != 0)
            strl = strl+"1";
          else
            strl = strl+"0";
        }
        return strl;
      }
    }
      

  2.   

    谢谢,我想要下面的格式。你写的我在《java编程思想》上看过
    public class Bitset {
        public static boolean test(long bin,int pos)
        {
        
        }
        public static void any(long bin)
        {
        
        }
        public static void none(long bin)
        {
        
        }
        public static void count(long bin)
        {
        
        }
        public static void size(long bin)
        {
        }
        public static void pos(long bin)
        {
        }
        public static void  filp(long bin)
        {
        }
        public static void filp(long bin,int pos)
        {
        }
        public static void set(long bin)
        {
        }
        public static void set(long bin,int pos)
        {
        }
        public static void reset(long bin)
        {
        }
        public static void reset(long bin,int pos)
        {
        }
        
    }
      

  3.   

    道理是一样的啊,比如public static boolean test(long bin,int pos)
    就可以写成
    public static boolean test(long bin,int pos) {
      int dex = pos;
      long l = bin;
      str = Test.pBinLong(l);//上面的方法还是要的
      if(dex<=0||dex>32) {
        System.out.println("指定位不符合范围");
        return false;
      }
      char cr = str.charAt(dex);
      if(cr=='1') {
        return true;
      }
      else {
        return false;
      }
      return true;
    }其它的对应下面的看看,就可以写了,很简单的说。
    //sigh,总不能自己什么都不写吧。//以下目标处理仅针对int而行,其它类型类同
        //no1
        int dex = 7;//指定位,int类型指定位不可大于32
        if(dex<=0||dex>32) {
          System.out.println("指定位不符合范围");
        }
        char cr = str.charAt(dex);
        if(cr=='1') {
          System.out.println("指定位"+dex+"是为"+cr);
        }
        else {
          System.out.println("指定位"+dex+"为"+cr+",不为1");
        }
        //no2
        dex = str.indexOf("0");
        if(dex==-1) {
          System.out.println("任意位都为1");
        }
        else {
          System.out.println("并非任意位都为1");
        }
        //no3
        dex = str.indexOf("1");
        if(dex==-1) {
          System.out.println("任意位都不为1");
        }
        else {
          System.out.println("并非任意位都不为1");
        }
        //no4
        int num = 0;
        System.out.println(str);
        int j = str.indexOf("1");
        System.out.println(j);
        while(str.indexOf("1")!=-1) {
          if(str.equals("1")) {
            num++;
            str = "";
          }
          else {
          dex = str.indexOf("1");
          str = str.substring(dex+1);
          num++;
          }
        }
        System.out.println("位置为1的个数是:"+num);