Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2018
    Thanked 0 Times in 0 Posts

    Need help with a bitshifting problem in java

    Hi, im just about done with an assignment and on the last question I have attempted to do it but I dont know if I used the bitshifting correctly.

    For the question, I had to extract the rightmost byte of an integer, then get the first 3 bits, next 3 bits, and last 2 bits of that rightmost byte and assign it to unsigned integer variables.

    What I have tried so far is:

    int rightmost = (y>>24)&0xFF // to get rightmost byte
    int first = (rightmost<<<1)&0xFF // to get first 3 bits of that byte
    int second = (rightmost >>> 3)&0xFF // to get next 3 bits
    int third = (rightmost >>>6)&0xFF // to get last 2 bits
    I know its probably incorrect use of the bitshifting but its what I thought I had to do.

  2. #2
    Regular Coder
    Join Date
    Sep 2014
    Thanked 55 Times in 53 Posts

    int rightmost = (y>>24) & 0xFF              // to get rightmost byte. mask for 8 bits is 0xff
    int first = (rightmost >> 5) & 0x07                    // to get first 3 bits of that byte. mask for 3 bits is 0x07
    int second = (rightmost >> 2) & 0x07                // to get next 3 bits
    int third = rightmost & 0x03                              // to get last 2 bits. mask for 2 bits 0x03
    Untested. Not sure if there is a way in Java to declare unsigned integer. But should be alright, as we are only manipulating a single byte.

    Edit, two days later. Sorry, looking at it again,to get the rightmost byte should just be (y & 0xff).
    Last edited by josephm; Sep 14th, 2018 at 03:36 AM. Reason: My mistake, misunderstood the requirement


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts