Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: Shifting Hex Values

  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Question Shifting Hex Values

    Hello, In my program I have a color hex value that I want to put an alpha mask on it. For Example

    int hex = 0xFFFFFF;
    int alphaHex = 0x00FFFFFF;

    I need to add the '00' in front of the hex, well it could be any value from 0-255 in hex form too. I read about using bitwise operators and stuff like that, but still a little confused about.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Shifting Hex Values

    Java Code:
    public class Foo {
        public static void main(String[] args) {
            int hex = 0xFFFFFF;
            int alphaHex = 0x00FFFFFF;
            
            System.out.println(hex == alphaHex);
        }
    }
    Perhaps you explain more precisely what you are trying to do. (Simple runnable code that doesn't produce the result you want would be good.)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Shifting Hex Values

    I need to add the '00' in front of the hex,
    Does the byte slot at the left(hi-order) of the number (an int with 4 bytes) have any contents IE its not zero? Like 0x12FFFFFF
    If you want to change the hi-order byte from its current contents to something else,
    AND with 0xFFFFFF to strip the hi-order byte then OR with 0xvv000000 to add on your desired byte
    where vv is the bit pattern you want in hex.

  4. #4
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default Re: Shifting Hex Values

    Quote Originally Posted by Norm View Post
    Does the byte slot at the left(hi-order) of the number (an int with 4 bytes) have any contents IE its not zero? Like 0x12FFFFFF
    If you want to change the hi-order byte from its current contents to something else,
    AND with 0xFFFFFF to strip the hi-order byte then OR with 0xvv000000 to add on your desired byte
    where vv is the bit pattern you want in hex.
    Yes I want to put a number at the left(hi-order) in the hex value, which can be any number not just zero. I'm still learning more about bytes so this might confuse me a little. Well I just found this java - How to shift bits in a color (hex value) - Stack Overflow and this person is trying to do the same thing I'm trying to do.

    XML Code:
    val = 0x04a8f5; //Your value 
    val |= 0xFF000000; //OR 0xFF000000 with your value, and assign the new value to val
    I read that you don't actualy shift the values, you change them? For example

    a hex value that is 0xFFFFFF is actually 0x00FFFFFF? So I would just be changeing the hi-order values using the OR operator? I'm new to this stuff.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Shifting Hex Values

    Most of the data you are dealing with is probably contained in an int (32bits or 8 hex digits)
    It is assumed that the high-order bits are 0s when you code 0xffff that is the same as 0x0000ffff
    You can use the OR operator to OR in new bits. If there are non-zero values there already, you might need to clear them before ORing in the new values. You clear bits by ANDing with 0s

  6. #6
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default Re: Shifting Hex Values

    OK Thanks for the help! I know understand this stuff better. Just one more question. When I convert a int to a hex value it, how do you add the leading zeros.
    Last edited by Kyle227; 01-09-2012 at 10:29 PM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Shifting Hex Values

    When I convert a int to a hex value it
    Are you asking about converting an int to a String of hex characters?
    There are no "hex" values as such. 0xFF is an int value of 255. The 0xFF is just an easier way to represent the number so you can see what bits are being used.

    The Integer.toHexString() method does not pad to the left with "0" characters, so if you want the full 8 hex characters, you will have to add them yourself. One way is to prepend an String of "0000000" and take the 8 rightmost characters.
    This might also be done with the formatting avaiable with the printf method.

  8. #8
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default Re: Shifting Hex Values

    Thanks again. So this is what I did and it worked.

    Java Code:
    int hex = 0xFFFFFF;
    int alpha = 200;
    long alphaHex;
    		
    	
    		
    alphaHex = Long.parseLong(String.format("%s000000", Integer.toHexString(alpha)), 16);
    System.out.println("Hex with alpha = " + Integer.toHexString(hex |= alphaHex));
    
    //outputs - Hex with alpha = c8ffffff

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Shifting Hex Values

    Instead of the awful parse/format call, try shifting:
    Java Code:
           int alphaHex = hex |  (200 << 24);
          System.out.println("alphaHex=" + Integer.toHexString(alphaHex));  // alphaHex=c8ffffff
    Last edited by Norm; 01-09-2012 at 11:34 PM.
    Kyle227 likes this.

  10. #10
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default Re: Shifting Hex Values

    Wow that's a lot easier, Well I guess I should learn more about those operators and stuff. Wait is this right. The << will shift the bits to the left 24 times, which is 6 bytes? When it shifts it, it puts the zeros in, which is 6 of them. Then the OR will change the left high order bytes.
    Last edited by Kyle227; 01-09-2012 at 11:47 PM.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Shifting Hex Values

    I should learn more about those operators
    Yes. Write a simple short program like you have posted, use the operators and then print the results using the toHexString() method.

    There are 8 bits per byte. 24/8 = 3

Similar Threads

  1. Shifting an Array
    By Zaibatsu in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 01:02 AM
  2. Shifting method using >>
    By ile4 in forum New To Java
    Replies: 7
    Last Post: 11-09-2010, 07:19 PM
  3. Shifting String Letters
    By DreamNaut in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 09:12 PM
  4. Bit Wise operation: shifting
    By rueter in forum New To Java
    Replies: 1
    Last Post: 06-27-2009, 07:51 AM
  5. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 11:19 PM

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
  •