0

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".

Community
  • 1
  • 1
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
  • It's the exact same question, so anyone who couldn't answer that question, won't be able to answer this one either. – finnw Feb 05 '10 at 21:21
  • The helpful advice in your linked article is in its comments: check the float you use, check its representation and then manipulate the bits accordingly (if the first 24 bits are mantissa then mask the first 24 bits and manipulate them). – Leonidas Feb 05 '10 at 21:22
  • No, that other question involves void-pointer casting, which would rely on deeper-level data representation. – java.is.for.desktop Feb 05 '10 at 21:24
  • Very few bitwise operations make sense on floating point data. Some masking tricks can help in some rare scenarios, but outside of quake's invSqrt trick, I've never seen any useful application of bitmagic on floats. It would help if you'd tell us what operations you want to do. – Ondergetekende Feb 05 '10 at 21:25
  • And, sadly (?!) some languages **don't** allow to cast floats from/to longs **without** changing the data representation. – java.is.for.desktop Feb 05 '10 at 21:26
  • I think that you are confusing two different types of video data. Scale-based data (coordinates, lengths, etc.) often has to be multiplied. *coded* data (color values, etc.) often have to be bit-masked. Does anything need *both*? – RBarryYoung Feb 05 '10 at 21:26
  • 1
    All bitwise operations will rely on underlying representation (which is well-defined), because bits only exist in the underlying representation. – Ondergetekende Feb 05 '10 at 21:27
  • @RBarryYoung: No, making a picture slightly brighter, applying gamma, all such things work better in floating point, and are more straight-forward: black is 0.0, white is 1.0. white * white = white. white * gray = gray. something * black = black. – java.is.for.desktop Feb 05 '10 at 21:30
  • Applying gamma is a mathematical operation involving floats, but there is no reason it can not use int based image data for inputs. What is it exactly that you're trying to accomplish? – recursive Feb 05 '10 at 21:57
  • If you have a floating point image, and you want to do the sort of thing you'd use XOR for on a bitmap, you use multiply-add; this is what an alpha channel does. So your question makes no sense... you're conflating two incompatible representations of images. – Andrew McGregor Feb 05 '10 at 22:17
  • It *is* the same as the alleged duplicate. Trying to pretend that you don't have to get access to the bits to manipulate the bits is wrongheaded. – dmckee --- ex-moderator kitten Feb 06 '10 at 03:21

2 Answers2

5

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
  • 83,943
  • 34
  • 151
  • 241
0

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.

Leonhardt Wille
  • 557
  • 5
  • 20