Total members 11895 |It is currently Sun Dec 22, 2024 10:21 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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

its o/p is 1 how?




Author:

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 .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 Arithmetic Operators in java     -  
 Relational Operators table     -  
 Bitwise operators in java     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com