bit operators

Discussion in 'C++' started by filox, Oct 30, 2006.

  1. filox

    filox Guest

    not sure is it a c++ or a c question, but since i'm using c++, i'll ask
    here...
    i would like to use bit operators (&, |, <<), but on the type double,
    because it has 64 bits. unfortunately, it seems i can't do that. can i
    somehow define my own type that has the desired number of bits (i.e. using
    bit fields) and then use those operators on that type?

    --
    You're never too young to have a Vietnam flashback
    filox, Oct 30, 2006
    #1
    1. Advertising

  2. filox

    Adrian Guest

    filox wrote:
    > not sure is it a c++ or a c question, but since i'm using c++, i'll ask
    > here...
    > i would like to use bit operators (&, |, <<), but on the type double,
    > because it has 64 bits. unfortunately, it seems i can't do that. can i
    > somehow define my own type that has the desired number of bits (i.e. using
    > bit fields) and then use those operators on that type?
    >

    How about using the stl bitset?

    #include <iostream>
    #include <bitset>

    int main(char *argv[], int argc)
    {
    std::bitset<64> b;

    b[34]=true;
    b[3]=true;

    std::cout << b << std::endl;

    b>>=5;
    std::cout << b << std::endl;

    b=255;
    std::cout << b << std::endl;

    b&=24;
    std::cout << b << std::endl;

    return 0;
    }


    Adrian
    Adrian, Oct 31, 2006
    #2
    1. Advertising

  3. filox wrote:
    > not sure is it a c++ or a c question, but since i'm using c++, i'll ask
    > here...
    > i would like to use bit operators (&, |, <<), but on the type double,
    > because it has 64 bits. unfortunately, it seems i can't do that. can i
    > somehow define my own type that has the desired number of bits (i.e. using
    > bit fields) and then use those operators on that type?
    >


    If all you need is 64 bits, most modern compiler have a built in type.

    i.e.

    typedef unsigned long long bits_64;

    If you need more you can define your own type e.g.

    struct bits128
    {
    bits_64 m_lo;
    bits_64 m_hi;
    };

    bits128 Bits( bits_64 lo, bits_64 hi )
    {
    bits128 retval = { lo, hi };
    return retval;
    }

    bits128 operator|( const & bits128 lhs, const & bits128 rhs )
    {
    bits128 retval = { lhs.lo | rhs.lo, lhs.hi, rhs.hi };
    }

    .... plus a whole bunch more like this for all the other operators you need.
    Gianni Mariani, Oct 31, 2006
    #3
  4. filox

    filox Guest

    thanks to everyone who replied!

    --
    You're never too young to have a Vietnam flashback
    filox, Oct 31, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrinalroy

    Bit wise shift operators

    mrinalroy, Oct 20, 2006, in forum: Java
    Replies:
    3
    Views:
    435
    IchBin
    Oct 20, 2006
  2. Replies:
    3
    Views:
    1,746
    Timothy Bendfelt
    Jan 19, 2007
  3. s.subbarayan
    Replies:
    4
    Views:
    364
    s.subbarayan
    Apr 7, 2005
  4. Replies:
    9
    Views:
    968
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    172
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page