Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to cast int to UInt32 bitwise?

i have some low level image/texture operations where 32-bit colors are stored as UInt32 or int and i need a really fast bitwise conversion between the two.

e.g.

 int color = -2451337;  

 //exception
 UInt32 cu = (UInt32)color;

any ideas?

thanks and regards

like image 222
thalm Avatar asked Aug 30 '10 13:08

thalm


2 Answers

int color = -2451337;
unchecked {
    uint color2 = (uint)color;
    // color2 = 4292515959
}
like image 84
sisve Avatar answered Nov 12 '22 01:11

sisve


BitConverter.ToUInt32(BitConverter.GetBytes(-2451337), 0)
like image 45
Jader Dias Avatar answered Nov 12 '22 00:11

Jader Dias