472,981 Members | 1,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

?bitwise operators

As a beginning student of C++, books reference "bitwise operators" and give
brief examples, but I have not read a good explanation of what they are used
for. One reference mentioned that they are used in hardware programming.

Are they used very often in routine C/C++ programming, and what for?

thanks,
MJH
Jul 22 '05 #1
4 2426
Mike Hodkin wrote:
As a beginning student of C++, books reference "bitwise operators" and give
brief examples, but I have not read a good explanation of what they are used
for. One reference mentioned that they are used in hardware programming.

Are they used very often in routine C/C++ programming, and what for?


Yes, they are "often" used.

Although there are alternatives (bit fields), some code uses bitwise
operators to manage "flags".

For example in the Posix open() call where you can set alternative modes
for openning a file. The "select" system call also uses a "bitmask" to
select which fd's are interesting.

They are also used when manipulating data, for example when you want to
convert from big endian to little endian.

Certain arithmentic operations are also much less expensive as bit
operations. For example, if you want a number that is the next largest
multiple of a power of 2 - ( 1 + ( ( N - 1 ) | ( ( 1 << P ) - 1 ) ).

It's also used in cryptography, the xor "^" operator is a very useful
transform since ( ( A^B ) ^B ) == A . This is also used in computation
of RAID checksums. If you look up "raidzz" it's somthing I wrote a few
years ago that makes used of bitwise functionality.

There are plenty more.

Jul 22 '05 #2

"Mike Hodkin" <mh*****@comcast.net> wrote in message
news:dO********************@comcast.com...
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are used in hardware programming.

Are they used very often in routine C/C++ programming, and what for?


Not as much as you would think.

The usually quoted use is for interfaceing to hardware but bitfields not
portable between different compilers because the standard doesn't mandate
how they are laid out or accessed (or it didn't - please feel free to
correct me on this) Consequently it is probably safer to stick to masks and
bit operators &,|,^

The only good use is if you really have to use the absolute minimum space
but even then it may not sit as well with class abstraction as bit masks -
consider standard i/o - iostate and fmtflags both use masks because they
have to for the interface and once they are defined for the interface it
would be foolish to use bitfields in the implementation

Nick
Jul 22 '05 #3

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:vE******************@news-binary.blueyonder.co.uk...

"Mike Hodkin" <mh*****@comcast.net> wrote in message
news:dO********************@comcast.com...
As a beginning student of C++, books reference "bitwise operators" and give
brief examples, but I have not read a good explanation of what they are

used
for. One reference mentioned that they are used in hardware programming.
Are they used very often in routine C/C++ programming, and what for?


Not as much as you would think.


Maybe not in your work. I use bitwise operators all the time.
The usually quoted use is for interfaceing to hardware but bitfields not
portable between different compilers because the standard doesn't mandate
how they are laid out or accessed (or it didn't - please feel free to
correct me on this) Consequently it is probably safer to stick to masks and bit operators &,|,^

The only good use is if you really have to use the absolute minimum space
but even then it may not sit as well with class abstraction as bit masks -
consider standard i/o - iostate and fmtflags both use masks because they
have to for the interface and once they are defined for the interface it
would be foolish to use bitfields in the implementation

Nick

Nick,
Where did "bitfields" come into this conversation? He asked about
"bitwise operators", which, to me, means the operators like & and |.
Nothing to do with bitfields.

To the original poster:

the reason bitwise operators are used for hardware is because hardware
is connected to the computer/processor via sets of wires, usually through
some kind of I/O port device. The hardware being addressed gets its control
informaton by which bits in that connecton are on (1) or off (0). And, the
easiest way to turn a bit on is to OR (|) it with a binary value which has
only that bit set. Likewise, to set a bit to zero, you would AND (&) it
with a binary value which has all bits *except* that one set.

It's also an easy (and compact) way to pass a set of booleans between
software objects. Instead of sending eight boolean values, you set the bits
of one unsigned char (for example), and send that in one shot.

-Howard



Jul 22 '05 #4

"Howard" <al*****@hotmail.com> wrote in message
news:bt********@dispatch.concentric.net...

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:vE******************@news-binary.blueyonder.co.uk...

"Mike Hodkin" <mh*****@comcast.net> wrote in message
news:dO********************@comcast.com...
As a beginning student of C++, books reference "bitwise operators" and

give
brief examples, but I have not read a good explanation of what they
are
used
for. One reference mentioned that they are used in hardware programming.
Are they used very often in routine C/C++ programming, and what for?


Not as much as you would think.


Maybe not in your work. I use bitwise operators all the time.
The usually quoted use is for interfaceing to hardware but bitfields not
portable between different compilers because the standard doesn't

mandate how they are laid out or accessed (or it didn't - please feel free to
correct me on this) Consequently it is probably safer to stick to masks

and
bit operators &,|,^

The only good use is if you really have to use the absolute minimum space but even then it may not sit as well with class abstraction as bit masks - consider standard i/o - iostate and fmtflags both use masks because they
have to for the interface and once they are defined for the interface it
would be foolish to use bitfields in the implementation

Nick


Nick,
Where did "bitfields" come into this conversation? He asked about
"bitwise operators", which, to me, means the operators like & and |.
Nothing to do with bitfields.


sorry - I miss read it.
Still - I did at least explain why bitwise operators are preferable to
bitfields
Jul 22 '05 #5

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

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
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...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
16
by: Santhosh | last post by:
Hi to all, How the individual digits of a number can be obtained using the bitwise operators alone.Is it possible to do it ? If we have n = 34 Result has to be 3,4. Thanks a billion for...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.