views:

303

answers:

2

Possible Duplicate:
how to perform bitwise operation on floating point numbers

Hello, everyone!

Background:
I know that it is possible to apply bitwise operation on graphics (for example XOR). I also know, that in graphic programs, graphic data is often stored in floating point data types (to be able for example to "multiply" the data with 1.05). So it must be possible to perform bitwise operations on floating point data, right?

I need to be able to perform bitwise operations on floating point data. I do not want to cast the data to long, bitwise manipulate it, and cast back to float.

I assume, there exist a mathematical way to achieve this, which is more elegant (?) and/or faster (?).

I've seen some answers but they could not help, including this one.

EDIT:
That other question involves void-pointer casting, which would rely on deeper-level data representation. So it's not such an "exact duplicate".

+4  A: 

By the time the "graphics data" hits the screen, none of it is floating point. Bitwise operations are really done on bit strings. Bitwise operations only make sense on numbers because of consistent encoding scheme to binary. Trying to get any kind of logical bitwise operations on floats other than extracting the exponent or mantissa is a road to hell.

Basically, you probably don't want to do this. Why do you think you do?

recursive
So casting to long, manipulating, and casting back is good enough and fast then?
java.is.for.desktop
recursive: Amen, Brother!
RBarryYoung
@java: what language? In C, for example, I don't believe there is any overhead whatsoever.
recursive
A: 

A floating point number is just another representation of a binary in memory, so you could:

  • measure the size of the data type (e.g. 32 bits), e.g. sizeof(pixel)
  • get a pointer to it - choose an integer type of the same size for that, e.g. UINT *ptr = &pixel
  • use the pointer's value, e.g. newpixel=(*ptr)^(*ptr)

This should at least work with non-negative values and should have no considerable calculative overhead, at least in an unmanaged context like C++. Maybe you have to mask out some bits when doing your operation, and - depending of the type - you may have to treat exponent and base separately.

LWille