Stephen K
Stephen K

Reputation: 737

Why can't I shift 0xff0000 to 0xff000000 using << 2?

isn't this literally just moving the FF two bits to the left? for some reason, doing this in javascript I get 0x3fc0000

What I am trying to do is combine a hex color with an alpha channel mask

i.e. alpha = 0xff / full opacity

color = 0x00ff00

end result = 0xff00ff00

Upvotes: 0

Views: 187

Answers (2)

Harry
Harry

Reputation: 11648

You need to move it more than two bits 2 bits. If you want to shift these you need to use

color << 8;

Try the following in js fiddle

var color = 0x00ff00;
alert(color.toString(16));
color = color << 8;
alert(color.toString(16));
color = color | 0xff;
alert(color.toString(16));
console.log(color);

Upvotes: 0

John Ledbetter
John Ledbetter

Reputation: 14183

f is 4 bits (1111 in binary). Two hexadecimal digits (0xff) is 8 bits. So you need to shift by 8 to move over two hexadecimal places.

Upvotes: 1

Related Questions