754 views

Consider a function is_greater() given below. Mark all option(s) which return $1$.

int is_greater(unsigned int x, unsigned int y)
{
if(x-y > 0)
return 1;
else
return 0;
}

1. is_greater$(1, 2)$
2. is_greater$(-1, -2)$
3. is_greater$(-2, -1)$
4. is_greater$(-1, 0)$

This is my approach edited
For this question, do we really need to first interpret the decimal numbers in binary and subtract in a cumbersome manner and then again interpret the result of the subtraction in decimal?

Case 1: x and y are signed numbers. We do x+y or x-y. Case 2: x and y are unsigned numbers and we do x+y or x-y.

The resulting bits in both the cases in binary are the same. SIgned/unsigned is used during integer promotion/extension. Since all of the numbers are int here, no extension will happen.

Also since everything is unsigned, it's sure to be positive and will surely be greater than zero(0).

Please correct me if I am wrong.
To be honest the first thing that strikes in our mind is solving the expression in decimal point of view and its natural in exam to avoid such lengthy procedures but we are here to learn how the things actually work

Since computers only understand binary and if i am not wrong this is how computers solve these kind of expressions  😁

## and greater than 0;

All the options are correct .

Now first try to learn how unsigned numbers are represented in the computer when we pass negative number as argument and it catch as an unsigned number.

Say My computer is using 32 bit to store an integer datatype.

now using 32 bits the largest unsigned number we can represent $2^{32}-1=4294967295$

now signed number was represented 2’s complement form in computer.

now 32 bit largest number we can represent :-$2^{31}-1=2147483647$

now how -1 is represented in signed form ?

=>(11111111111111111111111111111111) which is in 2’s complement form evaluated to -1.

now how -1 is evaluated in unsigned form?

=>(11111111111111111111111111111111) which evaluated to 4294967295.

same how -2 is is evaluated in unsigned form?

=> (11111111111111111111111111111110) which evaluated to 4294967294.

now one more point :-

difference of two unsigned number:-

the result of subtraction  is if comes negative we use modular arithmetic to convert it to unsigned

by the following rule (a-b)mod $2^{32}$ .

for example we have a=2,b=4

now a-b =-2 which will represent as $((-2)mod 2^{32})$=4294967294

now coming to the following question:-

option a:-

is_greater(1,2) is called.

now

x-y= -1 now result comes negative so it will represent as 4294967295.

which is greater than 0 so 1 will be returned.

option b:-

is_greater(-1,-2) is called.

now x=(-1) is represented as 4294967295.

now y=(-2) is represented as 4294967294.

the difference is 1 which is greater than 0.

so 1 will be returned.

option c:

is_greater(−2,−1) is called.

now x=(-2) is represented as 4294967294.

now y=(-1) is represented as 4294967295.

now x-y =-1 which again converted to $((-1)mod 2^{32})=4294967295.$ which is greater than 0 , so 1 will be returned.

option D:-

I believe now you can do it by yourself.

You are taking %d in scanf which is signed take %u then run the program see what output you will get.

when i put %u in scanf im getting the same output

what is the output u guys are expecting @Arjun sir any pair of equal number will produce 0 as output.

by
All coz in unsigned -1 is the largest value so obviously it would be greater than 0
by