如何在内部的8位二进制文件上执行左循环移位,它存储在数组中(How to perform left circular shift on an 8 bit binary internally which

编程入门 行业动态 更新时间:2024-10-28 00:21:40
如何在内部的8位二进制文件上执行左循环移位,它存储在数组中(How to perform left circular shift on an 8 bit binary internally which are stored in array)

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

假设a[i]={202,205} a[202]=11001010且a[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(“\ n循环移位:”); //打印获得的循环移位值(int h = 0; h System.out.print(“”); for(int index = 0; index <8; index ++)System.out.print(di [index]); }}}

This code is just printing the last bits of an array of size (s[i].length()). for suppose string is HELLO WORLD! its just printing ! binary value left rotated by one bit. Can someone help..please

suppose a[i]={202,205} a[202]=11001010 and a[205]=11000011 After Circular left Shift Output should be like 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; } } }

The actual code is:

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; }

List item

} 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("\n Circular shift:" ); //printing obtained circular shift values for(int h=0;h System.out. print(" “); for(int index=0;index<8;index++) System.out.print(di[index]); } } }

最满意答案

我构建了一个测试应用程序来创建一个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; } }

I built a test application to create a Java method that would shift the bits in an int array of length 8 one bit to the left in a circular pattern.

Here are the test results.

[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]

As you can see, the bits are shifted one bit to the left, and the left most bit is moved to the right.

And here's the test application code. It's short, self-contained, and most important, runnable.

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; } }

更多推荐

本文发布于:2023-08-05 15:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   二进制文件   如何在   left   perform

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!