简体   繁体   English

从Java中的各个位构造一个字节

[英]Construct a byte from individual bits in java

I'm currently writing a random number generator that generates a pseudorandom int and then returns an int representing the least significant bit in the int value. 我目前正在编写一个随机数生成器,该生成器生成一个伪随机整数,然后返回一个表示整数值中最低有效位的整数。

public class RNG {

    //algorithm input: first input is the seed
    private int x;

    //primes for calculating next random number
    private int p;
    private int q;

    //constructor
    public RNG()
    {
        p = getPrime();
        q = getPrime();

        //make sure p and q are not equal
        while(p == q)
        {
            q = getPrime();
        }

        //seed must not be divisible by p or q
        //easiest way is to make the seed another prime number
        //this prime does not need to be congruent to 3 mod 4, so
        //getPrime() is not used.

        x = (BigInteger.probablePrime(8, new Random())).intValue();
    }

    //return a pseudorandom prime number using java.util.Random such that
    //the number is congruent to 3 mod 4
    private int getPrime()
    {
        Random rand = new Random();
        int i;
        while(true)
        {
            //generates a random BigInteger. The probability
            //this BigInteger is composite is less than 2^-100
            //bitLength is 8 so maximum prime value is 251
            i = (BigInteger.probablePrime(8, rand)).intValue();
            if((i % 4) == (3 % 4))
            {
                break;
            }
        }

        return i;
    }

    public int next()
    {
        //calculate next algorithm input
        int next = (x * x) % (p * q);

        //set algorithm input for next next() call
        x = next;

        //return least significant bit
        return next & 1;
    }
}

The aim of the program is to be able to create a return value that a test class can then write to a file, and because the output is the least significant bit of the randomly generated number the output will be cryptographically secure. 该程序的目的是能够创建一个返回值,然后测试类可以将该返回值写入文件,并且由于输出是随机生成的数字的最低有效位,因此该输出将具有密码安全性。

However, when trying to come up with the implementation of the test class, I'm having trouble trying to figure out what to do with the output. 但是,当试图提出测试类的实现时,我很难弄清楚如何处理输出。 What I need the test class to be able to do is write 512 bits to an output file. 我需要测试类能够执行的操作是将512位写入输出文件。 The FileOuputStream class, however, is only capable of writing bytes to the output file. 但是,FileOuputStream类只能将字节写入输出文件。

What I'm trying to do is figure out if there's a way to group the individual 1 or 0 outputs from my random number generator into byte values, but so far I've not found anything that's supported in Java. 我想做的是找出是否有办法将随机数生成器的单个1或0输出分组为字节值,但是到目前为止,我还没有找到Java支持的任何内容。

You can construct bytes (or ints, or longs) using bit shifting with the << operator (or the bit shift and assignment operator <<= ) 您可以使用<<操作符(或位移和赋值操作符<<= )使用位移来构造字节(或int或longs)。

byte b = 0;
for (int i = 0; i < 8; i++) {
    b <<= 1;
    b |= rng.next();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM