Why is the output False if -8 & 7 is not 0? [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • Bitwise '&' operator

    6 answers




Why is the output False if -8 & 7 is not 0?
I know its supposed to be && but the question says "Is there a problem in the code? What is it? And why is the output False?" can someone help please.



#include <stdio.h>


int main ()
{
if (-8 & 7)
{
printf("Alwaysn");
}
else
{
printf("Falsen");
}
return 0;
}









share|improve this question









New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Groo, Lundin c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 15:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Convert -8 and 7 into bits and perform bitwise AND masking, you will get 0000, decimal Zero
    – Twinkle
    Nov 19 at 12:09










  • You put (-8 && 7) for logical AND operation
    – Twinkle
    Nov 19 at 12:11










  • I know its supposed to be &&...how?
    – Sourav Ghosh
    Nov 19 at 12:11










  • @Lundin: Statements addressing a person’s knowledge, as in “If you understand…,” unnecessarily address a person rather than subject matter and may be perceived as unkind. Such phrasing suggests the OP ought to have knowledge they may not have been taught yet and therefore has neglected some duty.
    – Eric Postpischil
    Nov 19 at 12:12












  • @VivekBhardwaj: The question states the OP knows that && was the originally intended operator but that they are deliberately asking about &.
    – Eric Postpischil
    Nov 19 at 12:13















up vote
-1
down vote

favorite













This question already has an answer here:




  • Bitwise '&' operator

    6 answers




Why is the output False if -8 & 7 is not 0?
I know its supposed to be && but the question says "Is there a problem in the code? What is it? And why is the output False?" can someone help please.



#include <stdio.h>


int main ()
{
if (-8 & 7)
{
printf("Alwaysn");
}
else
{
printf("Falsen");
}
return 0;
}









share|improve this question









New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Groo, Lundin c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 15:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Convert -8 and 7 into bits and perform bitwise AND masking, you will get 0000, decimal Zero
    – Twinkle
    Nov 19 at 12:09










  • You put (-8 && 7) for logical AND operation
    – Twinkle
    Nov 19 at 12:11










  • I know its supposed to be &&...how?
    – Sourav Ghosh
    Nov 19 at 12:11










  • @Lundin: Statements addressing a person’s knowledge, as in “If you understand…,” unnecessarily address a person rather than subject matter and may be perceived as unkind. Such phrasing suggests the OP ought to have knowledge they may not have been taught yet and therefore has neglected some duty.
    – Eric Postpischil
    Nov 19 at 12:12












  • @VivekBhardwaj: The question states the OP knows that && was the originally intended operator but that they are deliberately asking about &.
    – Eric Postpischil
    Nov 19 at 12:13













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • Bitwise '&' operator

    6 answers




Why is the output False if -8 & 7 is not 0?
I know its supposed to be && but the question says "Is there a problem in the code? What is it? And why is the output False?" can someone help please.



#include <stdio.h>


int main ()
{
if (-8 & 7)
{
printf("Alwaysn");
}
else
{
printf("Falsen");
}
return 0;
}









share|improve this question









New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












This question already has an answer here:




  • Bitwise '&' operator

    6 answers




Why is the output False if -8 & 7 is not 0?
I know its supposed to be && but the question says "Is there a problem in the code? What is it? And why is the output False?" can someone help please.



#include <stdio.h>


int main ()
{
if (-8 & 7)
{
printf("Alwaysn");
}
else
{
printf("Falsen");
}
return 0;
}




This question already has an answer here:




  • Bitwise '&' operator

    6 answers








c bitwise-operators






share|improve this question









New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 19 at 12:10









Sourav Ghosh

107k14129186




107k14129186






New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 12:04









Ron Uchitel

4




4




New contributor




Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ron Uchitel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by Groo, Lundin c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 15:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Groo, Lundin c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 15:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Convert -8 and 7 into bits and perform bitwise AND masking, you will get 0000, decimal Zero
    – Twinkle
    Nov 19 at 12:09










  • You put (-8 && 7) for logical AND operation
    – Twinkle
    Nov 19 at 12:11










  • I know its supposed to be &&...how?
    – Sourav Ghosh
    Nov 19 at 12:11










  • @Lundin: Statements addressing a person’s knowledge, as in “If you understand…,” unnecessarily address a person rather than subject matter and may be perceived as unkind. Such phrasing suggests the OP ought to have knowledge they may not have been taught yet and therefore has neglected some duty.
    – Eric Postpischil
    Nov 19 at 12:12












  • @VivekBhardwaj: The question states the OP knows that && was the originally intended operator but that they are deliberately asking about &.
    – Eric Postpischil
    Nov 19 at 12:13


















  • Convert -8 and 7 into bits and perform bitwise AND masking, you will get 0000, decimal Zero
    – Twinkle
    Nov 19 at 12:09










  • You put (-8 && 7) for logical AND operation
    – Twinkle
    Nov 19 at 12:11










  • I know its supposed to be &&...how?
    – Sourav Ghosh
    Nov 19 at 12:11










  • @Lundin: Statements addressing a person’s knowledge, as in “If you understand…,” unnecessarily address a person rather than subject matter and may be perceived as unkind. Such phrasing suggests the OP ought to have knowledge they may not have been taught yet and therefore has neglected some duty.
    – Eric Postpischil
    Nov 19 at 12:12












  • @VivekBhardwaj: The question states the OP knows that && was the originally intended operator but that they are deliberately asking about &.
    – Eric Postpischil
    Nov 19 at 12:13
















Convert -8 and 7 into bits and perform bitwise AND masking, you will get 0000, decimal Zero
– Twinkle
Nov 19 at 12:09




Convert -8 and 7 into bits and perform bitwise AND masking, you will get 0000, decimal Zero
– Twinkle
Nov 19 at 12:09












You put (-8 && 7) for logical AND operation
– Twinkle
Nov 19 at 12:11




You put (-8 && 7) for logical AND operation
– Twinkle
Nov 19 at 12:11












I know its supposed to be &&...how?
– Sourav Ghosh
Nov 19 at 12:11




I know its supposed to be &&...how?
– Sourav Ghosh
Nov 19 at 12:11












@Lundin: Statements addressing a person’s knowledge, as in “If you understand…,” unnecessarily address a person rather than subject matter and may be perceived as unkind. Such phrasing suggests the OP ought to have knowledge they may not have been taught yet and therefore has neglected some duty.
– Eric Postpischil
Nov 19 at 12:12






@Lundin: Statements addressing a person’s knowledge, as in “If you understand…,” unnecessarily address a person rather than subject matter and may be perceived as unkind. Such phrasing suggests the OP ought to have knowledge they may not have been taught yet and therefore has neglected some duty.
– Eric Postpischil
Nov 19 at 12:12














@VivekBhardwaj: The question states the OP knows that && was the originally intended operator but that they are deliberately asking about &.
– Eric Postpischil
Nov 19 at 12:13




@VivekBhardwaj: The question states the OP knows that && was the originally intended operator but that they are deliberately asking about &.
– Eric Postpischil
Nov 19 at 12:13












1 Answer
1






active

oldest

votes

















up vote
5
down vote














Why is the output False if -8 & 7 is not 0?




A: It is 0, actually.



To elaborate, & is the bit-wise AND operator, and you need to check for the binary representation of the arguments.



Considering two's complement representation,





  • -8 has a representation 1111111111111000


  • 7 has a representation 0000000000000111


According to the rule of bitwise AND, the produced result is 0, which evaluates to FALSY. So, in your case, the if-else condition boils down to



if (0)                     // this is FALSE 
{
printf("Alwaysn");
}
else // else block gets executed
{
printf("Falsen"); // so this is printed
}


That's why, the else block is executed and you see False being printed out.






share|improve this answer























  • While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
    – Eric Postpischil
    Nov 19 at 12:14










  • @EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
    – Lundin
    Nov 19 at 12:21






  • 1




    @Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
    – Eric Postpischil
    Nov 19 at 12:26










  • @EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
    – Lundin
    Nov 19 at 12:41






  • 2




    @Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
    – Eric Postpischil
    Nov 19 at 12:53


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote














Why is the output False if -8 & 7 is not 0?




A: It is 0, actually.



To elaborate, & is the bit-wise AND operator, and you need to check for the binary representation of the arguments.



Considering two's complement representation,





  • -8 has a representation 1111111111111000


  • 7 has a representation 0000000000000111


According to the rule of bitwise AND, the produced result is 0, which evaluates to FALSY. So, in your case, the if-else condition boils down to



if (0)                     // this is FALSE 
{
printf("Alwaysn");
}
else // else block gets executed
{
printf("Falsen"); // so this is printed
}


That's why, the else block is executed and you see False being printed out.






share|improve this answer























  • While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
    – Eric Postpischil
    Nov 19 at 12:14










  • @EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
    – Lundin
    Nov 19 at 12:21






  • 1




    @Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
    – Eric Postpischil
    Nov 19 at 12:26










  • @EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
    – Lundin
    Nov 19 at 12:41






  • 2




    @Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
    – Eric Postpischil
    Nov 19 at 12:53















up vote
5
down vote














Why is the output False if -8 & 7 is not 0?




A: It is 0, actually.



To elaborate, & is the bit-wise AND operator, and you need to check for the binary representation of the arguments.



Considering two's complement representation,





  • -8 has a representation 1111111111111000


  • 7 has a representation 0000000000000111


According to the rule of bitwise AND, the produced result is 0, which evaluates to FALSY. So, in your case, the if-else condition boils down to



if (0)                     // this is FALSE 
{
printf("Alwaysn");
}
else // else block gets executed
{
printf("Falsen"); // so this is printed
}


That's why, the else block is executed and you see False being printed out.






share|improve this answer























  • While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
    – Eric Postpischil
    Nov 19 at 12:14










  • @EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
    – Lundin
    Nov 19 at 12:21






  • 1




    @Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
    – Eric Postpischil
    Nov 19 at 12:26










  • @EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
    – Lundin
    Nov 19 at 12:41






  • 2




    @Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
    – Eric Postpischil
    Nov 19 at 12:53













up vote
5
down vote










up vote
5
down vote










Why is the output False if -8 & 7 is not 0?




A: It is 0, actually.



To elaborate, & is the bit-wise AND operator, and you need to check for the binary representation of the arguments.



Considering two's complement representation,





  • -8 has a representation 1111111111111000


  • 7 has a representation 0000000000000111


According to the rule of bitwise AND, the produced result is 0, which evaluates to FALSY. So, in your case, the if-else condition boils down to



if (0)                     // this is FALSE 
{
printf("Alwaysn");
}
else // else block gets executed
{
printf("Falsen"); // so this is printed
}


That's why, the else block is executed and you see False being printed out.






share|improve this answer















Why is the output False if -8 & 7 is not 0?




A: It is 0, actually.



To elaborate, & is the bit-wise AND operator, and you need to check for the binary representation of the arguments.



Considering two's complement representation,





  • -8 has a representation 1111111111111000


  • 7 has a representation 0000000000000111


According to the rule of bitwise AND, the produced result is 0, which evaluates to FALSY. So, in your case, the if-else condition boils down to



if (0)                     // this is FALSE 
{
printf("Alwaysn");
}
else // else block gets executed
{
printf("Falsen"); // so this is printed
}


That's why, the else block is executed and you see False being printed out.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 12:56

























answered Nov 19 at 12:07









Sourav Ghosh

107k14129186




107k14129186












  • While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
    – Eric Postpischil
    Nov 19 at 12:14










  • @EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
    – Lundin
    Nov 19 at 12:21






  • 1




    @Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
    – Eric Postpischil
    Nov 19 at 12:26










  • @EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
    – Lundin
    Nov 19 at 12:41






  • 2




    @Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
    – Eric Postpischil
    Nov 19 at 12:53


















  • While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
    – Eric Postpischil
    Nov 19 at 12:14










  • @EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
    – Lundin
    Nov 19 at 12:21






  • 1




    @Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
    – Eric Postpischil
    Nov 19 at 12:26










  • @EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
    – Lundin
    Nov 19 at 12:41






  • 2




    @Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
    – Eric Postpischil
    Nov 19 at 12:53
















While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
– Eric Postpischil
Nov 19 at 12:14




While two’s complement is the overwhelming representation in modern systems, it is not required by the standard, so one cannot assert that 1…1000 is the representation of −8 without including this implementation-dependent assumption.
– Eric Postpischil
Nov 19 at 12:14












@EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
– Lundin
Nov 19 at 12:21




@EricPostpischil Yeah we must absolutely teach newbies about fictional one's complement and signed magnitude computers.
– Lundin
Nov 19 at 12:21




1




1




@Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
– Eric Postpischil
Nov 19 at 12:26




@Lundin: We should teach newbies about deriving correct results from specifications. The fact that some old machines and implementations used one’s complement is less important than teaching that one constructs correct code by deriving behavior from specifications.
– Eric Postpischil
Nov 19 at 12:26












@EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
– Lundin
Nov 19 at 12:41




@EricPostpischil Yes we should absolutely teach newbies to read and consider the specification ISO 9899:2018 chapter 6.2.6.2. And then thoughtfully consider that int may have padding bits, negative zeroes and trap representations. This must be done during the first week in a beginner class.
– Lundin
Nov 19 at 12:41




2




2




@Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
– Eric Postpischil
Nov 19 at 12:53




@Lundin: One does not need to lay out all the history and alternatives while explaining at a level like this. A simple note that an assumption has been made suffices both to be correct and to illustrate correct reasoning from specifications.
– Eric Postpischil
Nov 19 at 12:53



Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$