Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bit operators

Reply
Thread Tools

bit operators

 
 
filox
Guest
Posts: n/a
 
      10-30-2006
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


 
Reply With Quote
 
 
 
 
Adrian
Guest
Posts: n/a
 
      10-31-2006
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
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      10-31-2006
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.

 
Reply With Quote
 
filox
Guest
Posts: n/a
 
      10-31-2006
thanks to everyone who replied!

--
You're never too young to have a Vietnam flashback


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 benn686@hotmail.com C++ 9 08-22-2007 12:13 AM
"LoadLibrary" of a 32 bit so with 64 bit java on a 64 bit machine markryde@gmail.com Java 3 01-19-2007 10:30 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments