Switch to full style
For C/C++ coders discussions and solutions
Post a reply

using bit-wise operators in c++

Thu Sep 08, 2011 7:51 am

Code:
main()
{
  
int c;
c=-2&&3&&1;
printf("%d",c);
getch();
}
 

its o/p is 1 how?



Re: how o/p is 1

Thu Sep 08, 2011 7:18 pm

First i want to remind you of two things :
First ll and && are logical operators , their results can be 1 (true) or 0 (false) , and you are using it on numbers
Code:
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/logande.htm


Second negative numbers in C++ is represented in 2's complement form.
For example (If we assume for simplicity using 1 byte)
-2 ==>-( 00000010)=11111101+1=11111110

using bit-wise operators like and & and | show different results :
Code:
#include<iostream>
#include<conio.h>
using namespace std;

void main()
{
int c;
c=(-2)&(3)&(1);
printf("%d",c);
getch();
}
 

11111110 AND 00000011 AND 00000001 = 00000000
the results is 0 not 1 .

Post a reply
  Related Posts  to : using bit-wise operators in c++
 PHP operators     -  
 Using Assignment Operators     -  
 shortcut operators     -  
 difference between the >> and >>> operators     -  
 Bitwise operators     -  
 Equality Operators     -  
 C++ Increment Operators     -  
 Comparison Operators in java     -  
 Arithmetic Operators in java     -  
 Relational Operators table     -