5 Replies Latest reply on Oct 9, 2009 4:46 PM by 807580

    inverting specific bits in short

    807580
      Hi everyone,

      So I'm trying to manipulate bits in a short value, but I've never actually messed with specific bits of a short before. I know a short is a 16 bit value, so I want to invert bits 11-8 ONLY...As far as I know, shifting bits is the way to go,right? I just want to grab bits 11-8 and check if it is even. I know I have to do something like : if(bits % 2 ==0) but I don't know how to grab those into one single value that I can check if its even or not. Also, if it IS even, I want to invert the bits.

      For example....
      I have a short that is 13344 which is a binary value of '0011 0100 0010 0000'
      if bits 11-8(which is the value 0100) IS EVEN, invert the bits...in which I would get 1011,
      i.e.: 0100 inverted is 1011

      In the end, I should get the value 0011 1011 0010 0000

      I know how to invert the bits using ~, so that isn't an issue.

      Any help would be much appreciated!!!! Thanks.
        • 1. Re: inverting specific bits in short
          807580
          Shift right is a good bet. I would try logical shift right (>>>) as it will introduce zeros on the left, not ones.

          [http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html]

          You don't need to do %2, since all you need to know is "is the last bit set."
          int i = something;
          byte valueICareAbout = (i >>> 8) & 0x0F; //only look at last 4 bits
          if ( (valueICareAbout & 0x01) != 0 ) { //then it's odd
          }
          • 2. Re: inverting specific bits in short
            807580
            guitarlady3000 wrote:
            So I'm trying to manipulate bits in a short value, I want to invert bits 11-8
            Works for me:
            ^=0xf00;
            C:> java GuitarLady
            11010000100000
            11101100100000
            • 3. Re: inverting specific bits in short
              807580
              Okay, I see how that could work. As far as inverting ONLY those 4 bits...and putting it back to original form...would it be something like this?
              byte trackBits = (byte) ((memadd >>> 8) & 0x0F); //only look at last 4 bits
                            if ( (trackBits & 0x01) == 0 ) //if its even
                            {
                                memadd = (short) (~memadd >>> 8);   //invert those 4 bits
                                memadd = (short) (memadd << 8);            //shift it back to original form
                            }
              OR am I just going to do this as tschodt has shown?

              memadd = ~0xF00; 
              //does that invert JUST those 4 bytes I want and keep all the other bits the way they are??

              Edited by: guitarlady3000 on Oct 9, 2009 9:27 AM
              • 4. Re: inverting specific bits in short
                807580
                guitarlady3000 wrote:
                Okay, I see how that could work. As far as inverting ONLY those 4 bits...and putting it back to original form...would it be something like this?
                byte trackBits = (byte) ((memadd >>> 8) & 0x0F); //only look at last 4 bits
                              if ( (trackBits & 0x01) == 0 ) //if its even
                              {
                                  memadd = (short) (~memadd >>> 8);   //invert those 4 bits
                                  memadd = (short) (memadd << 8);            //shift it back to original form
                              }
                OR am I just going to do this as tschodt has shown?

                memadd = ~0xF00; 
                //does that invert JUST those 4 bytes I want and keep all the other bits the way they are??
                That was not what I suggested.
                You want something like
                        if((memadd&0x100)==0)memadd^=0xf00;
                • 5. Re: inverting specific bits in short
                  807580
                  Ohhhh Okay gotcha...

                  See I've never used notation like that in java before so that is why I wasn't sure about this whole thing.

                  Looks great, and it should work

                  Thanks, you both helped!