473,399 Members | 3,302 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,399 software developers and data experts.

How & operaator works ?

I have following code .
unsigned int var1 = 5 ;

#define ISVAR (1 << 2)

Then what is output of
(ISVAR & var1) .

How to find it out on paper for various values of var1 says 4 , 1 , 2
ect .

Regards ,
Mangesh .

Oct 3 '06 #1
5 1485
mangesh wrote:
I have following code .
unsigned int var1 = 5 ;

#define ISVAR (1 << 2)

Then what is output of
(ISVAR & var1) .
this statement has no side effects and produces no output. (I'm
assuming
you meant to follow it with a ";" not a "."

& is the bitwise "and" operator. In the result a bit is set if both
corresponding
bits are set in the operands. For details read your text book.

How to find it out on paper for various values of var1 says 4 , 1 , 2
ect .

--
Nick Keighley
****Can anyone find the error
Yes:
Error on line 0: Lazy programmer.
(comp.lang.c++)

Oct 3 '06 #2

"mangesh" <ma************@walla.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>I have following code .
unsigned int var1 = 5 ;

#define ISVAR (1 << 2)

Then what is output of
(ISVAR & var1) .

How to find it out on paper for various values of var1 says 4 , 1 , 2
ect .

Regards ,
Mangesh .
& is logical AND.

So, (ISVAR & var1) is (1 << 2) AND 5, while
(1 << 2 ) is 4 (1 left shifted 2 times).

So, we have the logical AND of 4 and 5.
In binary, 4 is 100 and 5 is 101. So, finally,
100 AND 101 = 100 = 4.

Serafeim
Oct 3 '06 #3
Papastefanos Serafeim wrote:
"mangesh" <ma************@walla.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>I have following code .
unsigned int var1 = 5 ;

#define ISVAR (1 << 2)

Then what is output of
(ISVAR & var1) .

How to find it out on paper for various values of var1 says 4 , 1 , 2
ect .

Regards ,
Mangesh .

& is logical AND.
bitwise, not logical - otherwise the result would be a boolean
So, (ISVAR & var1) is (1 << 2) AND 5, while
(1 << 2 ) is 4 (1 left shifted 2 times).

So, we have the logical AND of 4 and 5.
In binary, 4 is 100 and 5 is 101. So, finally,
100 AND 101 = 100 = 4.

Serafeim
In other words,

(ISVAR & var1)

always evaluates to either 4 (1 << 2) or 0, based on whether or not
the requested bit is set in var1's value. This means it can be used
directly as a truth value in a condition (without having to compare
it to anything).
Oct 3 '06 #4
mangesh wrote:
I have following code .
unsigned int var1 = 5 ;

#define ISVAR (1 << 2)

Then what is output of
(ISVAR & var1) .

How to find it out on paper for various values of var1 says 4 , 1 , 2
ect .
http://www.gmonline.demon.co.uk/cscene/CS9/CS9-02.html
Oct 3 '06 #5
mangesh posted:
I have following code .
unsigned int var1 = 5 ;

#define ISVAR (1 << 2)

This is equal to 4.

Then what is output of
(ISVAR & var1) .

The following two bit-patterns are AND'ed with each other:

100
& 101
-----
100

The answer is 4.
How to find it out on paper for various values of var1 says 4 , 1 , 2
ect .
By reading up on bitwise manipulation, or it might be handier to copy your
buddy's homework.

--

Frederick Gotham
Oct 3 '06 #6

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

Similar topics

2
by: lobrys | last post by:
hi I build a VB .NET application that makes file access, environnemet acess, database access, etc.... If I ran the app locally, everything works....(normal) If I copy the app on a server, and...
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
12
by: ~~~ .NET Ed ~~~ | last post by:
Hi, I have a standalone XML file (with the appropriate xml document header) that works fine when I load it using XmlDocument. I can have child elements like this without problems: ...
1
by: st | last post by:
Hi, I'm using xmlDocument.Save(xmlTextWriter) to create an Excel-readable file. All works well, except where I've replaced the carriage return chars in the .innertext to XML-compliant " "; It...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
20
by: William | last post by:
Original question: "Give a one-line C expression to test whether a number is a power of 2. " Answer: if (x && !(x & (x-1)) == 0) My question: Why does this expression work?
6
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList =...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
0
by: Mudiya Dissa | last post by:
Hi, I have some code on my application like the following <% redirectUrl = "mypage.asp?type=100&id=something&view=10 'some more asp code
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.