Home > Back-end >  C a signed integer invert
C a signed integer invert

Time:11-18

 
#include

Int main ()

{

Int x=0 x80000000;

Int y=0 x80000001;

Printf (" x=% d, y=% d \ n ", x, y);

Printf (" (x)=% d, (-) y=% d \ n ", (x), (y));

If (x & gt; Y)

Printf (" x=% d & gt; Y=% d \ n ", x, y);

The else

Printf (" x=% d & lt; Y=% d \ n ", x, y);

If ((-) x & gt; (y))

Printf (" (x)=% d & gt; (y)=% d \ n ", (-) x (y));

The else

Printf (" (x)=% d & lt; (y)=% d \ n ", (-) x (y));

return 0;

}

 
C
$GCC test.
$./a.out

X=2147483648, y=2147483647

(x)=- 2147483648, (-) y=2147483647

X=- 2147483648 & lt; Y=- 2147483647

(x)=- 2147483648 & gt; (y)=2147483647

Very strange, why? X is the smallest integer, then he should still take the smallest signed integers, why is the last (-) x & gt; (y), clearly seen from - x and y values, is (y) & gt; (x),

CodePudding user response:

The int type is the largest integer 2147483647, there is no 2147483648

CodePudding user response:

Printf (" (x)=% ld & lt; (y)=% d \ n ", (-) x (y)); Try this line of code changed

CodePudding user response:

- automatic type x has been changed from int to unsigned int, value is 2147483648, so - x> - y, just print and regard it as an int

CodePudding user response:

The compiler issues, try to run the code on my side, the result is
X=2147483648, y=2147483647
(x)=- 2147483648, (-) y=2147483647
X=- 2147483648 & lt; Y=- 2147483647
(x)=- 2147483648 & lt; (-) y=2147483647

CodePudding user response:

reference poetryTang reply: 3/f
- automatic type x has been changed from int to unsigned int, value is 2147483648, so - x> - y, just when they see it as print int

Validation and doesn't seem to happen promotion
 # include & lt; Iostream> 
#include
#include
#include
#include

Using boost: : typeindex: : type_id_with_cvr;

Int main (int arg c, char * argv [])
{
Int x=0 x80000000;
Int y=0 x80000001;
Printf (" type x=% s, type y=% s \ n ",
Type_id_with_cvr & lt; The decltype (x) & gt; Pretty_name () (). C_str (),
Type_id_with_cvr & lt; The decltype (y) & gt; Pretty_name () (). C_str ());
Printf (" x=% d, y=% d \ n ", x, y);
Printf (" (x)=% d, (-) y=% d \ n ", (x), (y));
Printf (" type x=% s, type y=% s \ n ",
Type_id_with_cvr & lt; The decltype (-) x & gt; Pretty_name () (). C_str (),
Type_id_with_cvr & lt; The decltype (x-y) & gt; Pretty_name () (). C_str ());
Printf (" type x=% s, type y=% s \ n ",
Type_id_with_cvr & lt; The decltype ((x)) & gt; Pretty_name () (). C_str (),
Type_id_with_cvr & lt; The decltype ((y)) & gt; Pretty_name () (). C_str ());
If (x & gt; Y)
Printf (" x=% d & gt; Y=% d \ n ", x, y);
The else
Printf (" x=% d & lt; Y=% d \ n ", x, y);
If ((-) x & gt; (y))
Printf (" (x)=% d & gt; (y)=% d \ n ", (-) x (y));
The else
Printf (" (x)=% d & lt; (y)=% d \ n ", (-) x (y));
return 0;
}

 type x=int, type y=int 
X=2147483648, y=2147483647
(x)=- 2147483648, (-) y=2147483647
Type x=int, type y=int
Type x=int, type y=int
X=- 2147483648 & lt; Y=- 2147483647
(x)=- 2147483648 & lt; (y)=2147483647

CodePudding user response:

reference 5 floor alittlenewbiek reply:
Quote: refer to the third floor poetryTang response:
- automatic type x has been changed from int to unsigned int, value is 2147483648, so - x> - y, just when they see it as print int

Validation and doesn't seem to happen promotion
 # include & lt; Iostream> 
#include
#include
#include
#include

Using boost: : typeindex: : type_id_with_cvr;

Int main (int arg c, char * argv [])
{
Int x=0 x80000000;
Int y=0 x80000001;
Printf (" type x=% s, type y=% s \ n ",
Type_id_with_cvr & lt; The decltype (x) & gt; Pretty_name () (). C_str (),
Type_id_with_cvr & lt; The decltype (y) & gt; Pretty_name () (). C_str ());
Printf (" x=% d, y=% d \ n ", x, y);
Printf (" (x)=% d, (-) y=% d \ n ", (x), (y));
Printf (" type x=% s, type y=% s \ n ",
Type_id_with_cvr & lt; The decltype (-) x & gt; Pretty_name () (). C_str (),
Type_id_with_cvr & lt; The decltype (x-y) & gt; Pretty_name () (). C_str ());
Printf (" type x=% s, type y=% s \ n ",
Type_id_with_cvr & lt; The decltype ((x)) & gt; Pretty_name () (). C_str (),
Type_id_with_cvr & lt; The decltype ((y)) & gt; Pretty_name () (). C_str ());
If (x & gt; Y)
Printf (" x=% d & gt; Y=% d \ n ", x, y);
The else
Printf (" x=% d & lt; Y=% d \ n ", x, y);
If ((-) x & gt; (y))
Printf (" (x)=% d & gt; (y)=% d \ n ", (-) x (y));
The else
Printf (" (x)=% d & lt; (y)=% d \ n ", (-) x (y));
return 0;
}

 type x=int, type y=int 
X=2147483648, y=2147483647
(x)=- 2147483648, (-) y=2147483647
Type x=int, type y=int
Type x=int, type y=int
X=- 2147483648 & lt; Y=- 2147483647
(x)=- 2147483648 & lt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related