【开 Eclipse 版的999个理由 & 需要理由吗?】 http://community.csdn.net/Expert/topic/3472/3472604.xml?temp=.7752497

解决方案 »

  1.   

    String str="0001010101";
            char[] c=str.toCharArray();
            for(int i=0;i<c.length;i++){
                if(c[i]!='0'){
                    System.out.println(i+1);
                    break;
                }
      

  2.   

    我现在手上就是一个byte呀,怎么处理呢
      

  3.   

    String str="00101010";
            char[] c=str.toCharArray();
            for(int i=0;i<c.length;i++){
                if(c[i]=='1'){
                    System.out.println("u get it!");
                    break;
                }else{ continue; }
    }
      

  4.   

    谢谢6
    不过我要问的就是位操作
    谁写一个完整的Demo,ok?
    包括边界问题,异常,等等
      

  5.   

    import java.util.Random;public class Test {
        public static void main(String[] args) {
            byte n = (byte) new Random().nextInt(256);
            int r = 0x100;
            int index = -1;        for (int i = 0; i < 8; ++i) {
                r >>>= 1;
                if ((n & r) == 0) {
                    index = i;
                    break;
                }
            }        StringBuffer s = new StringBuffer(8);
            s.append(Integer.toBinaryString(n & 0xFF));
            int l = 8 - s.length();
            for (int i = 0; i < l; ++i) {
                s.insert(0, '0');
            }
            System.out.println("n = " + s.toString() + ", index = " + index);
        }
    }
      

  6.   

    随机产生一个8位的整数,然后从高位往低位判断,哪一位是0,则把位数输出来,以下是一些可能的结果:n = 01011001, index = 0
    n = 10000001, index = 1
    n = 00011000, index = 0
    n = 11111001, index = 5
    n = 00110000, index = 0
    n = 01011110, index = 0
    n = 10110011, index = 1
    n = 01110010, index = 0
    n = 11111100, index = 6
    n = 00100110, index = 0
      

  7.   

    你是要找从左到右还是从右到左的第一个为这0的位置呀?
    <当是从右到左找第一个不为0的位置>public class Demo
    {
       public static int aa() {
          byte  b = 55;
          if (b < 0) return 1;
          if ((b & 64) == 64) return 2;
          if ((b & 32) == 32) return 3;
          if ((b & 16) == 16) return 4;
          if ((b & 8) == 128) return 5;
          if ((b & 4) == 4) return 6;
          if ((b & 2) == 2) return 7;
          if ((b & 1) == 1) return 8;
          return 0;
       }
       
       public static void main(String[] args) {
          System.out.println(aa());
       }
    }大致意思是这样,算法可自己优化一下