1. ## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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. ## 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. ## Re: Shifting Hex Values

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.

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. ## 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. ## 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. ## 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. ## 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. ## 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.

10. ## 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. ## Re: Shifting Hex Values

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