bitwise & with a float?

This is a discussion on bitwise & with a float? within the C++ Programming forums, part of the General Programming Boards category; i've tried using bitwise & with two floats and it won't work. i looked on a google group how to ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    bitwise & with a float?

    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?

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Not that I know of, but some of the smarter people on this forum probably do.

    Cheers!

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by yahn
    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?
    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]).
    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.*

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    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.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    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

    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();
        }
    and that just output -1 8 times. what did i do wrong?
    Last edited by yahn; 12-30-2005 at 08:04 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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 08: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.*

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    ahh. thank you very much. i knew this wasn't as hard as i was making it to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21