开发者

How to shift bits in a color (hex value)

开发者 https://www.devze.com 2023-04-03 18:56 出处:网络
I have the following hex value store 开发者_如何学Goin a variable: 0x04a8f5 I want to convert the value to:

I have the following hex value store 开发者_如何学Goin a variable:

0x04a8f5

I want to convert the value to:

0xff04a8f5

How can I accomplish this? I've tried to do this by the following operation:

int result = 0x04a8f5 >> 8;


Use the following example as a guideline.

val = 0x04a8f5; //Your value
val |= 0xFF000000; //OR 0xFF000000 with your value, and assign the new value to val

Note, this isn't bit shifting because if your original value is a 32 bit (or larger) integer, then there is already a higher order byte available that can store the FF value. In other words, your original variable is actually 0x0004a8f5. Using an |= assignment will OR FF with the byte that you are wanting to change. No shifting necessary.

Also, shifting 0x0004a8f5 by 8 bits would result in 0x000004a8.


Because you want to prepend FF (1111 1111) to the front of your number, this isn't really a bit shift at all. You are just adding a constant to your color value.

As long as your color value is never going to take more than 6 hex digits to represent, you can just do:

color |= 0xFF000000

0

精彩评论

暂无评论...
验证码 换一张
取 消