Unsigned int and unsigned int contact with differentiated variables -C / C ++ program basis (V)

Examples of analysis

#include<iostream>
#include <stdio.h>

struct Result
{
	char c;
	char d;
	unsigned char e;
};

Result getChar(int x, int y)
{
	Result res;
	unsigned int a = x;
	(a + y > 10) ? (res.c = 1) : (res.c = 2);
	res.d = a + y;
	res.e = a + y;
	return res;
};

int main()
{
	Result res1;
	res1 = getChar(7, 4);
	printf("c1:%d\n", res1.c);
	printf("d1:%d\n", res1.d);
	printf("e1:%d\n", res1.e);

	printf("---------------------\n");

	Result res2;
	res2 = getChar(7, 3);
	printf("c2:%d\n", res2.c);
	printf("d2:%d\n", res2.d);
	printf("e2:%d\n", res2.e);

	printf("---------------------\n");

	Result res3;
	res3 = getChar(7, -7);
	printf("c3:%d\n", res3.c);
	printf("d3:%d\n", res3.d);
	printf("e3:%d\n", res3.e);

	printf("---------------------\n");

	Result res4;
	res4 = getChar(7, -10);
	printf("c4:%d\n", res4.c);
	printf("d4:%d\n", res4.d);
	printf("e4:%d\n", res4.e);

	system("pause");
	return 0;
}

operation result:

The above code logic is to enter into getChar x, y, and for adding, first x to type unsigned integer, and then by adding up to y, the final result stored in the variable d, the output out, If d is greater than 10 returns 1 if d is less than or equal to 10 2 return.

So the question is, at the input of the fourth group, d4 = -3, is less than 10, but why does the value of c is 1 then?

This comes to an unsigned integer data and differentiated and contact data of a signed integer.

There / unsigned integer defined

  • When no number (int) and has a number of symbols (unsignedInt) arithmetic symbols, there are a number (int) can be converted to non-symbol number (unsignedInt) symbol, then the corresponding arithmetic.
  • The difference between unsigned int and signed integer is unsigned types can be stored in the positive range than twice as large range of signed integers, as there are types of symbol bits to store the highest symbol without symbol all types of digital storage .
  • Unsigned integer can not store negative numbers, but the maximum value storage can be doubled.

With / without converting the integer data symbols

Yes -> No: 1) the most significant bit is 1 if not, to take complement; 2) If the most significant bit is not 0, stored directly.

Take complement: 1) is positive, it is in itself; 2) is negative, then the sign bit unchanged, the rest of you negated, the last one by one.

Yes / No calculation data unsigned integer

Further need to know is , when there is an expression symbol type and unsigned types of variables, all operands are calculated automatically converted to unsigned .

 

Then return to this problem in the past, when the input (7, -7), when converted into a very large -7 unsigned type, and then after adding 7, just at the edge of a spill, the equivalent was just neutralized, d is 0, so the returned or 1 c.

But when the input (7, -10), when the result of addition of the remaining -3 is converted to an unsigned integer, is a big number (e = 253), certainly greater than 10, then, there have been cases of the fourth group, the returned result is -3, but c is 1.

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104881658