gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > no binary OR on a float?

#75902 - racarate - Thu Mar 16, 2006 10:33 pm

Hi, I wrote a function to blend two colors


unsigned short blendColor( unsigned short colorA, unsigned short colorB, float colorRatio )
{

colorA = ( colorRatio * ( colorA & 0xF000 ) ) | ( colorA & 0x0FFF );
colorA = ( colorRatio * ( colorA & 0x0F00 ) ) | ( colorA & 0xF0FF );
colorA = ( colorRatio * ( colorA & 0x00F0 ) ) | ( colorA & 0xFF0F );
colorA = ( colorRatio * ( colorA & 0x000F ) ) | ( colorA & 0xFFF0 );

colorRatio = 1 - colorRatio;

colorB = ( colorRatio * ( colorA & 0xF000 ) ) | ( colorA & 0x0FFF );
colorB = ( colorRatio * ( colorA & 0x0F00 ) ) | ( colorA & 0xF0FF );
colorB = ( colorRatio * ( colorA & 0x00F0 ) ) | ( colorA & 0xFF0F );
colorB = ( colorRatio * ( colorA & 0x000F ) ) | ( colorA & 0xFFF0 );

return ( colorA + colorB );

}


And while I think my logic is sound, I am getting the following error:

error: invalid operands to binary |

What is the implicit cast that occurs when a short is multiplied by a float? If it resolves to a float, why can't I use a binary OR on it?
_________________
Neko: I love meow-tain climbing!
Need anything? I'll even save your game!

#75915 - poslundc - Thu Mar 16, 2006 11:35 pm

racarate wrote:
What is the implicit cast that occurs when a short is multiplied by a float? If it resolves to a float, why can't I use a binary OR on it?


Because bitwise-OR isn't intended to be used with floating-point representations of numbers, and many hardware platforms (though not the GBA, I suppose) have dedicated floating-point registers that you would not be able to use the OR operation on.

But as the FAQ says, don't use floating point, use fixed-point.

Dan.

#75923 - tepples - Fri Mar 17, 2006 1:07 am

I could convert this code to fixed-point, but it wouldn't work as expected on the Game Boy Advance or Nintendo DS. For one thing, you're blending groups of 4 bits, which works on the Apple IIGS, Sega Game Gear, Sega Genesis, and PC Engine, but not on Nintendo systems. The Super NES, GBC, GBA, and Nintendo DS use groups of 5 bits.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#75949 - racarate - Fri Mar 17, 2006 5:39 am

Ack, there is a bunch of things wrong with this. Thanks for the help, I think I know how to proceed now.
_________________
Neko: I love meow-tain climbing!
Need anything? I'll even save your game!