# Shifting Hex Values

• 01-09-2012, 06:23 AM
Kyle227
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.
• 01-09-2012, 06:29 AM
pbrockway2
Re: Shifting Hex Values
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.)
• 01-09-2012, 02:13 PM
Norm
Re: Shifting Hex Values
Quote:

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.
• 01-09-2012, 05:34 PM
Kyle227
Re: Shifting Hex Values
Quote:

Originally Posted by Norm
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.

Quote:

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.
• 01-09-2012, 05:44 PM
Norm
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
• 01-09-2012, 10:24 PM
Kyle227
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.
• 01-09-2012, 10:32 PM
Norm
Re: Shifting Hex Values
Quote:

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.
• 01-09-2012, 11:22 PM
Kyle227
Re: Shifting Hex Values
Thanks again. So this is what I did and it worked.

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```
• 01-09-2012, 11:31 PM
Norm
Re: Shifting Hex Values
Instead of the awful parse/format call, try shifting:
Code:

```      int alphaHex = hex |  (200 << 24);       System.out.println("alphaHex=" + Integer.toHexString(alphaHex));  // alphaHex=c8ffffff```
• 01-09-2012, 11:40 PM
Kyle227
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.
• 01-09-2012, 11:59 PM
Norm
Re: Shifting Hex Values
Quote: