473,378 Members | 1,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

bit operators

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
Oct 30 '06 #1
3 1515
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<64b;

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
Oct 31 '06 #2
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.

Oct 31 '06 #3
thanks to everyone who replied!

--
You're never too young to have a Vietnam flashback
Oct 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
6
by: bearophileHUGS | last post by:
Sometimes I suggest to add things to the language (like adding some set methods to dicts), but I've seen that I tend to forget the meaning of six set/frozenset operators: s & t s &= t s | t s...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
0
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
18
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.