i've tried using bitwise & with two floats and it won't work. i looked on a google group how to do it and it looked like you had to mess with the compiler. is there any simple way to do it?
i've tried using bitwise & with two floats and it won't work. i looked on a google group how to do it and it looked like you had to mess with the compiler. is there any simple way to do it?
Not that I know of, but some of the smarter people on this forum probably do.
Cheers!
bitwise operators are only defined for integral types, and do not work for floating pointing types.
You can do some hackery with typecasts on pointers (eg assign an integer pointer so it contains the address of a float, and then do bitwise operations on the integer). Unfortunately, the result of doing that falls into the domain of either implementation defined or undefined behaviour.
First, what are you really trying to do? Would manipulating the bits of a float do something that makes sense? If so, you are likely messing with the object representation of a floating point value, and as such would be best served by using an unsigned char -- or pointer thereto -- to manipulate the object representation. Perhaps provide a sample of code of your attempt (and please use code tags: [code][/code]).Originally Posted by yahn
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Make an artificial float and preform operations on each one of thoes.
[code]
int fn[10];
int fn2[10];
fn[1]=1;
fn2[1]=1
cout <<fn[1] << " " << fn2[1];
[code]
Its a total pain, but its pritty much the only way to do it that will work.
If you understand how floating-point numbers are stored, you should be able to extract the individual bytes (or words). You'll need to look-up the specifics for your particular compiler. You'll need to use pointers, and you'll probably have to typecast the pointers (to int-pointers) Once you have the individual bytes/words you can perform the bitwise operations.
If you don't understand how floating point numbers are stored in memory, you shouldn't be messing with the bits!
99 percent of the time, bitwise operations are used on variables that conceptually represent bit patterns, rather than numerical values. And, we generally use hex values to describe the bit pattern because it's very easy (for humans) to convert between hex and binary.
hmm. i want to do something pretty simple, thats why it's frustrating it's so hard to do. I want to save a number between 0 - 4294967296 as 4 bytes in a file. I've done this before in an interpreted language, and it was farely simple. I don't really know much about floating points, but i was lead to believe that they can hold numbers between 0 - 4294967296. i know a float has enough bytes to store numbers that large, but i don't know if it will work in a float now that i think about it. can anyone help me out, i don't see how this can be as hard as i'm making it.
edit::
so i guess i'm dumb and i can just save it in a unsigned long int -- since i don't want to store anything with a decimal anyways. and i guess i can use bitwise operators on unsigned long int. hmm. now i know why it was so frustrating.
edit::
i tryed this
and that just output -1 8 times. what did i do wrong?Code:unsigned long int j = 4294967295; stack<char> bytes; char i; for (i = 0; i < 4; i++) { char temp = 255 & j; bytes.push(temp); j = j >> 8; cout << static_cast<int>(temp) << endl; } for (i = 0; i < 4; i++) { cout << static_cast<int>(bytes.top()) << endl; bytes.pop(); }
Last edited by yahn; 12-30-2005 at 09:04 PM.
Not know that char and unsigned char may be different?Code:#include <iostream> #include <stack> #include <cstddef> #include <climits> using namespace std; int main() { unsigned long int j = ULONG_MAX; stack<unsigned char> bytes; for ( size_t i = 0; i < sizeof j; i++ ) { unsigned char temp = UCHAR_MAX & j; bytes.push(temp); j >>= CHAR_BIT; cout << static_cast<int>(temp) << endl; } for ( size_t i = 0; i < sizeof j; i++ ) { cout << static_cast<int>(bytes.top()) << endl; bytes.pop(); } return 0; } /* my output 255 255 255 255 255 255 255 255 */
Last edited by Dave_Sinkula; 12-30-2005 at 09:16 PM. Reason: :o Ooops & fixup.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
ahh. thank you very much. i knew this wasn't as hard as i was making it to be.