random byte to int java









up vote
1
down vote

favorite












  1. In the Random class, define a nextByte method that returns a value of the primitive type
    byte. The values returned in a sequence of calls should be uniformly distributed over all the
    possible values in the type.

  2. In the Random class, define a nextInt method that returns a value of the primitive type
    int. The values returned in a sequence of calls should be uniformly distributed over all the possible
    values in the type.
    (Hint: Java requires implementations to use the twos-complement representation for integers.
    Figure out how to calculate a random twos-complement representation from four random byte
    values using Java’s shift operators.)

Hi I was able to do part 3 and now I need to use 3. to solve 4. but I do not know what to do. I was thinking of using nextByte to make an array of 4 bytes then would I take twos complement of each so I wouldn't have negative numbers and then I would put them together into one int.
byte bytes = 42,-15,-7, 8 Suppose nextByte returns this bytes.
Then I would take the twos complement of each which i think would be 42, 241, 249, 8. Is this what it would look like and why doesn't this code work:



public static int twosComplement(int input_value, int num_bits)
int mask = (int) Math.pow(2, (num_bits - 1));
return -(input_value & mask) + (input_value & ~mask);



Then I would use the following to put all four bytes into an int, would this work:



int i= (bytes[0]<<24)&0xff000000|
(bytes[1]<<16)&0x00ff0000|
(bytes[2]<< 8)&0x0000ff00|
(bytes[3]<< 0)&0x000000ff;


Please be as specific as possible.










share|improve this question























  • Just take the four random bytes and combine them as in your last statement. Get rid of the twosComplement method, it's just a complicated NO-OP.
    – James K Polk
    Nov 9 at 19:31















up vote
1
down vote

favorite












  1. In the Random class, define a nextByte method that returns a value of the primitive type
    byte. The values returned in a sequence of calls should be uniformly distributed over all the
    possible values in the type.

  2. In the Random class, define a nextInt method that returns a value of the primitive type
    int. The values returned in a sequence of calls should be uniformly distributed over all the possible
    values in the type.
    (Hint: Java requires implementations to use the twos-complement representation for integers.
    Figure out how to calculate a random twos-complement representation from four random byte
    values using Java’s shift operators.)

Hi I was able to do part 3 and now I need to use 3. to solve 4. but I do not know what to do. I was thinking of using nextByte to make an array of 4 bytes then would I take twos complement of each so I wouldn't have negative numbers and then I would put them together into one int.
byte bytes = 42,-15,-7, 8 Suppose nextByte returns this bytes.
Then I would take the twos complement of each which i think would be 42, 241, 249, 8. Is this what it would look like and why doesn't this code work:



public static int twosComplement(int input_value, int num_bits)
int mask = (int) Math.pow(2, (num_bits - 1));
return -(input_value & mask) + (input_value & ~mask);



Then I would use the following to put all four bytes into an int, would this work:



int i= (bytes[0]<<24)&0xff000000|
(bytes[1]<<16)&0x00ff0000|
(bytes[2]<< 8)&0x0000ff00|
(bytes[3]<< 0)&0x000000ff;


Please be as specific as possible.










share|improve this question























  • Just take the four random bytes and combine them as in your last statement. Get rid of the twosComplement method, it's just a complicated NO-OP.
    – James K Polk
    Nov 9 at 19:31













up vote
1
down vote

favorite









up vote
1
down vote

favorite











  1. In the Random class, define a nextByte method that returns a value of the primitive type
    byte. The values returned in a sequence of calls should be uniformly distributed over all the
    possible values in the type.

  2. In the Random class, define a nextInt method that returns a value of the primitive type
    int. The values returned in a sequence of calls should be uniformly distributed over all the possible
    values in the type.
    (Hint: Java requires implementations to use the twos-complement representation for integers.
    Figure out how to calculate a random twos-complement representation from four random byte
    values using Java’s shift operators.)

Hi I was able to do part 3 and now I need to use 3. to solve 4. but I do not know what to do. I was thinking of using nextByte to make an array of 4 bytes then would I take twos complement of each so I wouldn't have negative numbers and then I would put them together into one int.
byte bytes = 42,-15,-7, 8 Suppose nextByte returns this bytes.
Then I would take the twos complement of each which i think would be 42, 241, 249, 8. Is this what it would look like and why doesn't this code work:



public static int twosComplement(int input_value, int num_bits)
int mask = (int) Math.pow(2, (num_bits - 1));
return -(input_value & mask) + (input_value & ~mask);



Then I would use the following to put all four bytes into an int, would this work:



int i= (bytes[0]<<24)&0xff000000|
(bytes[1]<<16)&0x00ff0000|
(bytes[2]<< 8)&0x0000ff00|
(bytes[3]<< 0)&0x000000ff;


Please be as specific as possible.










share|improve this question















  1. In the Random class, define a nextByte method that returns a value of the primitive type
    byte. The values returned in a sequence of calls should be uniformly distributed over all the
    possible values in the type.

  2. In the Random class, define a nextInt method that returns a value of the primitive type
    int. The values returned in a sequence of calls should be uniformly distributed over all the possible
    values in the type.
    (Hint: Java requires implementations to use the twos-complement representation for integers.
    Figure out how to calculate a random twos-complement representation from four random byte
    values using Java’s shift operators.)

Hi I was able to do part 3 and now I need to use 3. to solve 4. but I do not know what to do. I was thinking of using nextByte to make an array of 4 bytes then would I take twos complement of each so I wouldn't have negative numbers and then I would put them together into one int.
byte bytes = 42,-15,-7, 8 Suppose nextByte returns this bytes.
Then I would take the twos complement of each which i think would be 42, 241, 249, 8. Is this what it would look like and why doesn't this code work:



public static int twosComplement(int input_value, int num_bits)
int mask = (int) Math.pow(2, (num_bits - 1));
return -(input_value & mask) + (input_value & ~mask);



Then I would use the following to put all four bytes into an int, would this work:



int i= (bytes[0]<<24)&0xff000000|
(bytes[1]<<16)&0x00ff0000|
(bytes[2]<< 8)&0x0000ff00|
(bytes[3]<< 0)&0x000000ff;


Please be as specific as possible.







java random byte bitwise-operators twos-complement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 2:57

























asked Nov 9 at 2:32









M.Ruz

62




62











  • Just take the four random bytes and combine them as in your last statement. Get rid of the twosComplement method, it's just a complicated NO-OP.
    – James K Polk
    Nov 9 at 19:31

















  • Just take the four random bytes and combine them as in your last statement. Get rid of the twosComplement method, it's just a complicated NO-OP.
    – James K Polk
    Nov 9 at 19:31
















Just take the four random bytes and combine them as in your last statement. Get rid of the twosComplement method, it's just a complicated NO-OP.
– James K Polk
Nov 9 at 19:31





Just take the four random bytes and combine them as in your last statement. Get rid of the twosComplement method, it's just a complicated NO-OP.
– James K Polk
Nov 9 at 19:31













1 Answer
1






active

oldest

votes

















up vote
1
down vote













The assignment says that Java already uses two's complement integers. This is a useful property that simplifies the rest of the code: it guarantees that if you group together 32 random bits (or in general however many bits your desired output type has), then this covers all possible values exactly once and there are no invalid patterns.



That might not be true of some other integer representations, which might only have 2³²-1 different values (leaving an invalid pattern that you would have to avoid) or have 2³² valid patterns but both a "positive" and a "negative" zero, which would cause a random bit pattern to have a biased "interpreted value" (with zero occurring twice as often as it should).



So that it not something for you to do, it is a convenient property for you to use to keep the code simple. Actually you already used it. This code:



int i= (bytes[0]<<24)&0xff000000|
(bytes[1]<<16)&0x00ff0000|
(bytes[2]<< 8)&0x0000ff00|
(bytes[3]<< 0)&0x000000ff;


Works properly thanks to those properties. By the way it can be simplified a bit: after shifting left by 24, there is no more issue with sign-extension, all the extended bits have been shifted out. And shifting left by 0 is obviously a no-op. So (bytes[0]<<24)&0xff000000 can be written as (bytes[0]<<24), and (bytes[3]<< 0)&0x000000ff as bytes[3]&0xff. But you can keep it as it was, with the nice regular structure.
The twosComplement function is not necessary.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219019%2frandom-byte-to-int-java%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    The assignment says that Java already uses two's complement integers. This is a useful property that simplifies the rest of the code: it guarantees that if you group together 32 random bits (or in general however many bits your desired output type has), then this covers all possible values exactly once and there are no invalid patterns.



    That might not be true of some other integer representations, which might only have 2³²-1 different values (leaving an invalid pattern that you would have to avoid) or have 2³² valid patterns but both a "positive" and a "negative" zero, which would cause a random bit pattern to have a biased "interpreted value" (with zero occurring twice as often as it should).



    So that it not something for you to do, it is a convenient property for you to use to keep the code simple. Actually you already used it. This code:



    int i= (bytes[0]<<24)&0xff000000|
    (bytes[1]<<16)&0x00ff0000|
    (bytes[2]<< 8)&0x0000ff00|
    (bytes[3]<< 0)&0x000000ff;


    Works properly thanks to those properties. By the way it can be simplified a bit: after shifting left by 24, there is no more issue with sign-extension, all the extended bits have been shifted out. And shifting left by 0 is obviously a no-op. So (bytes[0]<<24)&0xff000000 can be written as (bytes[0]<<24), and (bytes[3]<< 0)&0x000000ff as bytes[3]&0xff. But you can keep it as it was, with the nice regular structure.
    The twosComplement function is not necessary.






    share|improve this answer
























      up vote
      1
      down vote













      The assignment says that Java already uses two's complement integers. This is a useful property that simplifies the rest of the code: it guarantees that if you group together 32 random bits (or in general however many bits your desired output type has), then this covers all possible values exactly once and there are no invalid patterns.



      That might not be true of some other integer representations, which might only have 2³²-1 different values (leaving an invalid pattern that you would have to avoid) or have 2³² valid patterns but both a "positive" and a "negative" zero, which would cause a random bit pattern to have a biased "interpreted value" (with zero occurring twice as often as it should).



      So that it not something for you to do, it is a convenient property for you to use to keep the code simple. Actually you already used it. This code:



      int i= (bytes[0]<<24)&0xff000000|
      (bytes[1]<<16)&0x00ff0000|
      (bytes[2]<< 8)&0x0000ff00|
      (bytes[3]<< 0)&0x000000ff;


      Works properly thanks to those properties. By the way it can be simplified a bit: after shifting left by 24, there is no more issue with sign-extension, all the extended bits have been shifted out. And shifting left by 0 is obviously a no-op. So (bytes[0]<<24)&0xff000000 can be written as (bytes[0]<<24), and (bytes[3]<< 0)&0x000000ff as bytes[3]&0xff. But you can keep it as it was, with the nice regular structure.
      The twosComplement function is not necessary.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        The assignment says that Java already uses two's complement integers. This is a useful property that simplifies the rest of the code: it guarantees that if you group together 32 random bits (or in general however many bits your desired output type has), then this covers all possible values exactly once and there are no invalid patterns.



        That might not be true of some other integer representations, which might only have 2³²-1 different values (leaving an invalid pattern that you would have to avoid) or have 2³² valid patterns but both a "positive" and a "negative" zero, which would cause a random bit pattern to have a biased "interpreted value" (with zero occurring twice as often as it should).



        So that it not something for you to do, it is a convenient property for you to use to keep the code simple. Actually you already used it. This code:



        int i= (bytes[0]<<24)&0xff000000|
        (bytes[1]<<16)&0x00ff0000|
        (bytes[2]<< 8)&0x0000ff00|
        (bytes[3]<< 0)&0x000000ff;


        Works properly thanks to those properties. By the way it can be simplified a bit: after shifting left by 24, there is no more issue with sign-extension, all the extended bits have been shifted out. And shifting left by 0 is obviously a no-op. So (bytes[0]<<24)&0xff000000 can be written as (bytes[0]<<24), and (bytes[3]<< 0)&0x000000ff as bytes[3]&0xff. But you can keep it as it was, with the nice regular structure.
        The twosComplement function is not necessary.






        share|improve this answer












        The assignment says that Java already uses two's complement integers. This is a useful property that simplifies the rest of the code: it guarantees that if you group together 32 random bits (or in general however many bits your desired output type has), then this covers all possible values exactly once and there are no invalid patterns.



        That might not be true of some other integer representations, which might only have 2³²-1 different values (leaving an invalid pattern that you would have to avoid) or have 2³² valid patterns but both a "positive" and a "negative" zero, which would cause a random bit pattern to have a biased "interpreted value" (with zero occurring twice as often as it should).



        So that it not something for you to do, it is a convenient property for you to use to keep the code simple. Actually you already used it. This code:



        int i= (bytes[0]<<24)&0xff000000|
        (bytes[1]<<16)&0x00ff0000|
        (bytes[2]<< 8)&0x0000ff00|
        (bytes[3]<< 0)&0x000000ff;


        Works properly thanks to those properties. By the way it can be simplified a bit: after shifting left by 24, there is no more issue with sign-extension, all the extended bits have been shifted out. And shifting left by 0 is obviously a no-op. So (bytes[0]<<24)&0xff000000 can be written as (bytes[0]<<24), and (bytes[3]<< 0)&0x000000ff as bytes[3]&0xff. But you can keep it as it was, with the nice regular structure.
        The twosComplement function is not necessary.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 20:57









        harold

        40.9k356107




        40.9k356107



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219019%2frandom-byte-to-int-java%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus