473,837 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

?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 2492
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*****@comcas t.net> wrote in message
news:dO******** ************@co mcast.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***@blueyond er.co.uk> wrote in message
news:vE******** **********@news-binary.blueyond er.co.uk...

"Mike Hodkin" <mh*****@comcas t.net> wrote in message
news:dO******** ************@co mcast.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*****@hotmai l.com> wrote in message
news:bt******** @dispatch.conce ntric.net...

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

"Mike Hodkin" <mh*****@comcas t.net> wrote in message
news:dO******** ************@co mcast.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
2536
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 of 'flags' for users on a website. The function below is supposed to be a search function for one of those flags in a particular thing. The idea is that when something calls userId(), it should get to see what type of user flags that are...
11
9037
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 anyone would use them - any real world examples or ideas? Examples follow (that I am reading in my Core JavaScript Guide 1.5). 15 & 9 yields 9 (1111 & 1001 = 1001) 15 | 9 yields 15 (1111 | 1001 = 1111) 15 ^ 9 yields 6 (1111 ^ 1001 = 0110) in...
6
8984
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 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the settings of the message is a matter of using the bitwise operators. This was a good practical example of...
2
3473
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
37
11084
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 anyone that could tell me how to convert these two things? 1)
5
5805
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 you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
2
2755
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 int column used for bitwise data - you know the sort of thing... 0, 1, 2, 4, 8, 16 etc intBitwise strName ----------------------
29
5962
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 often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something oft done in Python either. It seems like this occasional usage wouldn't justify having built-in...
16
3017
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 your reply in advance.
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9686
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9416
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5675
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5855
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.