in Programming in C retagged by
802 views
5 votes
5 votes

Which of the following expressions will evaluate to true?
INT_MIN is the minimum signed integer in the system which is using 2's complement representation for signed integers and variables are defined as follows:

int x = 5, i = -7;
  1. ((x >> 2) << 2) <= x
  2. ~ 1&& 1
  3. INT_MIN == -INT_MIN
  4. -10 < i <-1
in Programming in C retagged by
802 views

1 Answer

10 votes
10 votes

int x = 5, i = – 7;

  1. ((x>>2)<<2)<=x
    5 in 4-bit system is 0101
    (0101 >>2) <<2   
    ( 0001 ) << 2
    0100  which is 4
    4 <= 5 is True
     
  2. ~1 && 1
    1 : 0001         ~1: 1110       Both are non-zero
    So    ~1 && 1  is True 
  1. INT_MIN is the minimum signed number in the system. 
    So in a 4 bit system,  INT_MIN = 1000
    Now,  – INT_MIN = 2’s Complement of INT_MIN = 1000
    Hence,   INT_MIN == – INT_MIN  is True  
  1.  −10 < i < −1
    Relational operators ( <, <=, >, >= ) are Left to Right associative 
    So,  (( −10 < – 7 ) < −1 )  
           ( 1 < – 1 )  is False

4 Comments

Considering 4-bit signed system
1 is 0001  and ~1 is 1110
So when converted to decimal, ~1 is actually -2
Hope that clears the confusion.
0
0

Oh, looking at the question after days, I thought it was about -1 && 1 :P

Correct @mayur_dp

@Arpon Notice that it is a logical AND operator and not bitwise AND, I'm sure you mistook it for bitwise AND

So it's like ~1 && 1

= -2 && 1

= True ^ True (^ is conjunction, every number other than 0 is True)

= True

= 1 (in C Programming)

Since, in C Programming, logical operators return 1 (for true) or 0 (for false) as a result.

 

 

0
0
how is it coming -2? 1110 when converted to decimal is giving -6 right?
0
0
Answer:

Related questions