473,386 Members | 1,609 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,386 software developers and data experts.

The & Operator

Hello,

I'm having trouble understanding what K&R are saying: 'The bitwise AND
operator & is often used to mark off some set of bits, for example,

n = n % 0177;

sets to zero all but the low-order 7 bits of n.'

So in the end, what actually happens? What does the value of n become?

Dec 31 '05 #1
3 1571
And could someone give me an example of when the & operator is useful
or needed?

Dec 31 '05 #2
"Albert" <al*****************@gmail.com> writes:
I'm having trouble understanding what K&R are saying: 'The bitwise AND
operator & is often used to mark off some set of bits, for example,

n = n % 0177;
You mean "n = n & 0177";
sets to zero all but the low-order 7 bits of n.'

So in the end, what actually happens? What does the value of n become?


Think of the value of n as binary. 0177 is an octal constant; in
binary, it's 01111111. Ideally, n should be unsigned (bitwise operators
normally shouldn't be used with signed types).

For single-bit operands, the "&" operator is defined as:
0 & 0 == 0
0 & 1 == 0
1 & 0 == 0
1 & 1 == 1

Suppose the value of n (in binary) is 00010100111001111000101101001100
(32 bits). Then and-ing that value with 0177 yields:

00010100111001111000101101001100
& 00000000000000000000000001111111
--------------------------------
00000000000000000000000001001100

As you can see, this does just what K&R say it does: it zeros all but
the low-order 7 bits of n.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 31 '05 #3
Albert <al*****************@gmail.com> schrieb:
And could someone give me an example of when the & operator is useful
or needed?


I guess you mean a real-world example ...

Think of some code-base that uses for example the following definition
for error values to set or return:

// Error-categories
#define CAT_FILEOP 0x0100
#define CAT_WHATEVER 0x0200

#define ERR_NOSUCHFILE (CAT_FILEOP|0x01) // '|' means OR where '&' means AND
#define ERR_FILEEXISTS (CAT_FILEOP|0x02)

// ...

#define ERR_OUTOFMEM (CAT_WHATEVER|0x01)

Then you can use:

void check_error(int _err)
{
int cat,err;

cat = _err&0xf00; // mask out the specific error bits and get the category
err = _err&0x0ff; // mask out the category bits and get the error
hope("this helped"); // ;-)
}

Markus
Dec 31 '05 #4

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

Similar topics

1
by: learning_C++ | last post by:
Hi, I compiled some code. In the function friend ostream& operator<<(ostream& os, const complex c); I use the later argument complex c and complex& c. I can get the same values and there is no...
8
by: Jef Driesen | last post by:
I'm implementing some image processing algorithms in C++. I created a class called 'image' (see declaration below), that will take care of the memory allocations and some basic (mathematical)...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
1
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.