java 中的二进制序列使用按位运算符
This binary series in java using bitwise operators
请参考此版本的编辑,因为以前的版本不明确 - 谢谢
我希望生成一个依赖于字符串中的数字的二进制序列。
有2种数字。 7 和 9 和 非 7 和 非 9。
因此,例如数字 29 将具有与 39、49 或 59 完全相同的输出,但 79 因为两个数字都在 7 和 9 之下。
我们就称它为;
第 1 组:非 7 岁和 9 岁
第 2 组:7 岁和 9 岁
字符串 22,33, 23,42,21 等将提供相同的输出,因为所有数字组合都属于第 1 组。
字符串 77、79、99、97 将提供相同的输出,因为所有数字组合都属于第 2 组。
第 1 组和第 2 组的区别在于有效位数。
第 1 组有 3 个位,我们将在第 2 组有 4 个位。
所以数字 29 会产生
2 = 001
9 = 0001
这是所需的输出。
String s = "23";
输出:
001 001
001 010
001 100
010 001
010 010
010 100
100 001
100 010
100 100
另一个例子:
String s = "93";
输出:
0001 001
0001 010
0001 100
0010 001
0010 010
0010 100
0100 001
0100 010
0100 100
1000 001
1000 010
1000 100
同样,
String s = "274";
输出:
001 0001 001
001 0001 010
001 0001 100
001 0010 001 // the group 1 digits will complete the circulation of 3 bits where as group 2 digits will complete for 4 bits.
...
我们可以将它们存储在不同的数组中,打印它们等。我会照顾其余部分,但我现在似乎唯一想不通的是如何生成这样的输出。
任何帮助将不胜感激。
所以...我认为不需要按位运算符。如果你想做位级的东西,你可以用可以移动的东西替换 g1 和 g2 数组。至于 "counting",您是从 1 开始计算 2 的幂(以 10 为基数,您将得到 1、2、4、8 ...)。如果您更喜欢将其称为一次移动 1 位,也可以。
无论如何,这符合您的想法吗?
示例输出
$ java Foo
digits=27
001 0001
001 0010
001 0100
001 1000
010 0001
010 0010
010 0100
010 1000
100 0001
100 0010
100 0100
100 1000
$
Foo.java
public class Foo {
static String g1[] = { "001", "010", "100" };
static String g2[] = { "0001", "0010", "0100", "1000" };
static void blarch( String digits ) {
System.out.println("digits="+digits);
int N = digits.length(); // use N for max index
String sequence[][] = new String[ N ][];
int counts[] = new int[ N ];
for( int i = 0; i < N; ++i ) {
char c = digits.charAt(i);
sequence[i] = ( c == '7' || c == '9' ) ? g2 : g1;
counts[i] = 0; // emphasize we're counting from zero.
}
boolean printStuff = true;
while( printStuff ) {
for( int i = 0; i < N; ++i ) {
if( i >= 1 ) System.out.print( " " );
//System.out.print( "<i="+i+", counts[i]="+counts[i]+">");
System.out.print( sequence[i][ counts[i] ] );
}
System.out.println();
// increment, watch for overflow - done if we "overflow" on seq[0];
for( int i = N - 1; i >= 0; --i ) {
if( ++counts[i] < sequence[i].length ) break;
// overflow, we're done when seq#0 overflows.
if( i == 0 ) printStuff = false;
counts[i] = 0; // start this 'sequence' over at zero
}
}
}
public static void main(String args[]) {
blarch( "27" );
//blarch( "274" );
}
}
请参考此版本的编辑,因为以前的版本不明确 - 谢谢
我希望生成一个依赖于字符串中的数字的二进制序列。
有2种数字。 7 和 9 和 非 7 和 非 9。
因此,例如数字 29 将具有与 39、49 或 59 完全相同的输出,但 79 因为两个数字都在 7 和 9 之下。
我们就称它为;
第 1 组:非 7 岁和 9 岁
第 2 组:7 岁和 9 岁
字符串 22,33, 23,42,21 等将提供相同的输出,因为所有数字组合都属于第 1 组。
字符串 77、79、99、97 将提供相同的输出,因为所有数字组合都属于第 2 组。
第 1 组和第 2 组的区别在于有效位数。 第 1 组有 3 个位,我们将在第 2 组有 4 个位。
所以数字 29 会产生
2 = 001
9 = 0001
这是所需的输出。
String s = "23";
输出:
001 001
001 010
001 100
010 001
010 010
010 100
100 001
100 010
100 100
另一个例子:
String s = "93";
输出:
0001 001
0001 010
0001 100
0010 001
0010 010
0010 100
0100 001
0100 010
0100 100
1000 001
1000 010
1000 100
同样,
String s = "274";
输出:
001 0001 001
001 0001 010
001 0001 100
001 0010 001 // the group 1 digits will complete the circulation of 3 bits where as group 2 digits will complete for 4 bits.
...
我们可以将它们存储在不同的数组中,打印它们等。我会照顾其余部分,但我现在似乎唯一想不通的是如何生成这样的输出。
任何帮助将不胜感激。
所以...我认为不需要按位运算符。如果你想做位级的东西,你可以用可以移动的东西替换 g1 和 g2 数组。至于 "counting",您是从 1 开始计算 2 的幂(以 10 为基数,您将得到 1、2、4、8 ...)。如果您更喜欢将其称为一次移动 1 位,也可以。
无论如何,这符合您的想法吗?
示例输出
$ java Foo
digits=27
001 0001
001 0010
001 0100
001 1000
010 0001
010 0010
010 0100
010 1000
100 0001
100 0010
100 0100
100 1000
$
Foo.java
public class Foo {
static String g1[] = { "001", "010", "100" };
static String g2[] = { "0001", "0010", "0100", "1000" };
static void blarch( String digits ) {
System.out.println("digits="+digits);
int N = digits.length(); // use N for max index
String sequence[][] = new String[ N ][];
int counts[] = new int[ N ];
for( int i = 0; i < N; ++i ) {
char c = digits.charAt(i);
sequence[i] = ( c == '7' || c == '9' ) ? g2 : g1;
counts[i] = 0; // emphasize we're counting from zero.
}
boolean printStuff = true;
while( printStuff ) {
for( int i = 0; i < N; ++i ) {
if( i >= 1 ) System.out.print( " " );
//System.out.print( "<i="+i+", counts[i]="+counts[i]+">");
System.out.print( sequence[i][ counts[i] ] );
}
System.out.println();
// increment, watch for overflow - done if we "overflow" on seq[0];
for( int i = N - 1; i >= 0; --i ) {
if( ++counts[i] < sequence[i].length ) break;
// overflow, we're done when seq#0 overflows.
if( i == 0 ) printStuff = false;
counts[i] = 0; // start this 'sequence' over at zero
}
}
}
public static void main(String args[]) {
blarch( "27" );
//blarch( "274" );
}
}