The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
22 views

what is the output of the following programming????

and am confusing is unsigned int stores signed integer, and 

what is ‘ ’ this symbol ?? and what happened when assigning ‘~0’ to y????? and 

what will be printed when x,y are printing and how …?

can anyone tell me how it happened ..Thankyou…..!

#include<stdio.h>

int  main()

{

   unsigned int x = -1;

   int y = ~0;

   if (x == y)

      printf("same");

   else

      printf("not same");

printf("\n x is %u, y is %u", x, y);

   return 0;

}

 
asked in Programming by (171 points) | 22 views

1 Answer

0 votes
1) When you assign the sign bit to unsigned bit then first converted to 2s complement

-1 => 1| 1 where first 1 is sign and second represent value

Now applying sign bit extension rule so 1111...1 upto the size/capacity of x

2) y=~0 means flip bits and represent in 2s complement i.e -(0+1) = -1

3) x==y then from y=-1 it is converted to unsigned which is 1111.1 upto the size/capacity of y which is same as size taken by x

Hence x and y contains same value

so same is printed

And output in 64 bit compiler is 4294967295 4294967295 here also converting signed to unsigned for y
answered by Junior (789 points)
edited by
0

nice explanation.....,but am not understanding how 2's complement of 0 will -1. i think 2's complement of 0 is again 0.

0

https://stackoverflow.com/questions/791328/how-does-the-bitwise-complement-operator-tilde-work

read this how '~' working

Conclusion for ~ is that

~N = -(N+1)

0
okay thank you.......!

Related questions

+2 votes
1 answer
2
asked Sep 10, 2017 in Programming by atul_21 Active (2.7k points) | 135 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,808 questions
54,481 answers
188,249 comments
74,526 users