如何在内部的8位二进制文​​件上执行左循环移位,存储在数组中

时间:2016-05-11 04:18:57

标签: java

此代码只是打印大小(s[i].length())数组的最后几位。假设字符串是HELLO WORLD!,它只是打印!二进制值左旋一位。有人可以帮忙..请

假设a[i]={202,205} a[202]=11001010a[205]=11000011 圆形左移后输出应该是 a[202]=10010101 a[205]=10000111

for (i = 0; i < s[i].length(); i++) { //loop for computing binary value
    System.out.print(" " + dt[i] + " = " );
    int  g=7;
    while(dt[i]!=0)                             
    {
          bindt[g]=Math.abs((dt[i])%2);
          dt[i]=(dt[i])/2;
          g=g-1;

    }
    for(g=0;g<8;g++)
        System.out.print(bindt[g]); 
    }
}
for(i=0;i<s[i].length();i++)          //loop for circular shift on bits
{
    var=bindt[0];
    for(int index=0;index<8;index++)           
    {
        di[index]=bindt[index+1];
        if(index==7)                    
           di[index]=var;
        } 
    }
}

实际代码是:

    int power = 0,temp;
    int[] a= new int[1000];
    int[] m=new int[1000];
    int[] dt=new int[1000];
    int[] bindt=new int[100];
    int[] ds=new int[1000];
    int[] di=new int[50];
    int[] cov=new int[100];
    int arr[]=new int[10];  
   int var;
    String d = null;
    String e= null;
    int bin[]=new int[8];
    int bink[]=new int[8];
    JFileChooser chooser = new JFileChooser();    //  Browse File
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    fileName = file.getPath();
     String fn= file.getName();
     System.out.println("File name is :"+fn);
    FileReader fileReader = new FileReader(fileName);
    BufferedReader br = new BufferedReader(fileReader);
    while ((line = br.readLine()) != null)
    {
        System.out.println(line);
        l = line.length();
        System.out.println("length is " + l);
         System.out.print("Plain Text={");
        while ((br.readLine() != null) || (i <= l))    //computing plain text
        {
            s[i] = line;
             i++;
         }
        //counting no of vowels,consonants,special symbols and spaces
        for (i = 0; i < s[i].length(); i++) {
            char ch = line.charAt(i);
            if (ch=='e'||ch=='a'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
                vowels++;
            else if (line.charAt(i)==' ')
                spaces++;
            else if   (ch=='>'||ch=='+'||ch=='}'||ch=='/'||ch=='*'||ch=='!'||ch=='@'||ch=='#'||ch=='$'||ch=='%'||
                    ch=='^'||ch=='&'||ch=='('||ch==')'||ch=='"'||ch==':'||ch==';'||
                    ch=='{'||ch=='['||ch==']'||ch=='?'||ch==','||ch=='.'||ch=='<')
                      ss++; 
            else
                consonants++; 
            ascii = (int) ch;
            a[i] = ascii;
            System.out.print(+a[i]+",");
        }
        System.out.print("}");
        System.out.print("\n");
        System.out.print("Static text={");
        for (i = 0; i < s[i].length(); i++) {
            m[i]=(254 - ((a[i]) - (14)));             //Generating static text
                            System.out.print( + m[i]+",");
        }
        System.out.print("}");
        System.out.print("\n");            
        System.out.println("X=number of vowels = "+vowels);
        System.out.println("W=number of consonants = "+consonants);
        System.out.println("Z=number of special symbols = "+ss);
        System.out.println("Y=number of spaces = "+spaces);
        System.out.println("N=Total number of characters = "+l);
        Denominator = ((l-consonants)+(l-vowels)+(l-spaces)+(l-ss));
        System.out.println("Denominator is :"+Denominator);
         System.out.print("Binary Value is: ");
         j=7;
                //loop for finding binary number
                while(l!=0)                            // binary number computation
                          {
                             bin[j]=Math.abs(l%2);
                             l=l/2;
                             j=j-1;
                        }
                        for(j=0;j<8;j++)
                              System.out.print(bin[j]);
                       // loop for obtaining 1's complement          
                        for(j=0;j<8;j++)                                        
                             {
                                if(bin[j]==1)
                                     bin[j]=0;
                                else
                                    bin[j]=1;
                             }
                            System.out.print("\n");
                            System.out.println("Complemented value:")
                            d=Arrays.toString(bin).trim().replace(",","").replace("[","").replace("]","").replace(" ","").trim();
                            System.out.println(d);
                            System.out.print("Numerator: ");
                            Numerator = Integer.parseInt(d,2);       //Computing k value
                            System.out.println(Numerator);
                            k=((Numerator)/(Denominator));
                            System.out.println("K =" +k);
                            System.out.print("Dynamic Text:{");   //Generating dynamic text
                            for (i = 0; i < s[i].length(); i++) 
                            {
                            dt[i]=((m[i])+(k));
                            System.out.print( + dt[i]+",");
                             }
                            z=7;
                            while(k!=0)                             
                          {
                             bink[z]=Math.abs(k%2);
                             k=k/2;
                             z=z-1;
                          }
                            System.out.println("}");
                            System.out.println("Binary Value of k is: ");
                             for(z=0;z<8;z++)
                              System.out.print(bink[z]); 
                               }
                            System.out.println("ASCII Values of Dynamic Text is:");
                            for (int h = 0; h < s[i].length(); h++)      //loop for computing binary value
                            {
                           System.out.print(" " + dt[h] + " = " );
                             int  g=7;
                             while(dt[h]!=0)  {
                              bindt[g]=(Math.abs((dt[h])%2));
                               dt[h]=(dt[h])/2;
                             g=g-1;
                              }
  • 列出项目

                    }
                            for(g=0;g<8;g++)
    
                              System.out.print(bindt[g]);                               
                            }
               }
               for(int h=0;h<s[i].length();h++)          //loop for circular shift 
                            {
                  for(int index=0;index<8;index++)           
                        {
                            di[index]=bindt[index+1];
                               if(index==7)                    
                                      di[index]=bindt[0];
                              } 
                              }
    

    System.out.println(&#34; \ n循环移位:&#34;); //打印获得的循环移位值     for(int h = 0; h        System.out中。打印(&#34;“);
          for(int index = 0; index&lt; 8; index ++)     是System.out.print(二[指数]);      }     }   }

2 个答案:

答案 0 :(得分:0)

我构建了一个测试应用程序来创建一个Java方法,该方法将长度为8的int数组中的位以一个圆形模式向左移动一位。

以下是测试结果。

[1, 1, 0, 0, 1, 0, 1, 0] -> [1, 0, 0, 1, 0, 1, 0, 1]
[1, 1, 0, 0, 0, 0, 1, 1] -> [1, 0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 1, 1] -> [0, 0, 0, 0, 0, 1, 1, 0]

如您所见,这些位向左移位一位,最左侧位移到右侧。

这是测试应用程序代码。它简短,独立,最重要的是,可运行

package com.ggl.testing;

import java.util.Arrays;

public class BitShifting {

    public static void main(String[] args) {
        BitShifting bitShifting = new BitShifting();

        int[] bits1 = { 1, 1, 0, 0, 1, 0, 1, 0 };
        shiftAndDisplay(bitShifting, bits1);
        int[] bits2 = { 1, 1, 0, 0, 0, 0, 1, 1 };
        shiftAndDisplay(bitShifting, bits2);
        int[] bits3 = { 0, 0, 0, 0, 0, 0, 1, 1 };
        shiftAndDisplay(bitShifting, bits3);
    }

    private static void shiftAndDisplay(BitShifting bitShifting, int[] bits) {
        int[] shifted = bitShifting.shiftLeft(bits);
        System.out.print(Arrays.toString(bits));
        System.out.print(" -> ");
        System.out.println(Arrays.toString(shifted));
    }

    public int[] shiftLeft(int[] bits) {
        int bit = bits[0];
        int length = bits.length - 1;
        int[] output = new int[bits.length];

        for (int i = 0; i < length; i++) {
            output[i] = bits[i + 1];
        }

        output[length] = bit;

        return output;
    }

}

答案 1 :(得分:0)

8位上的循环移位:

byte rotateLeft(byte b) {
    boolean highBit = b < 0;
    b <<= 1;
    if (highBit) {
        b |= 1;
    }
    return b;
}

这利用了该字节在两个补码-128 .. 127中,并且只有负字节的高位设置。对于 int n,必须强制转换为(byte)n