分类: C/C++
2009-06-20 21:45:00
减法和乘法的运算结果类似。
如果作为signed int型数据的b=-130,b与立即数之间操作时不影响b的类型,运算结果仍然为signed int型:
signed int b=-130;
std::cout<输出为-100。
而对于浮点数来说,浮点数(float,double)实际上都是有符号数,unsigned 和signed前缀不能加在float和double之上,当然就不存在有符号数根无符号数之间转化的问题了。
#include
#include
#define UNSIGNED_SHORT_LT(a , b) (((unsigned short) (((unsigned short) a) - ((unsigned short) b))) > (unsigned short) 0x7fff)
#define UNSIGNED_COMP_LT(a , b) ((unsigned int) (((unsigned int) a) - ((unsigned int) b)))
typedef struct test_node {
int num;
struct test_node *next;
} test_node_t;
test_node_t *head;
test_node_t *tail;
void build_link(int num)
{
if (head == NULL) {
test_node_t *tmp = (test_node_t *)malloc(sizeof(test_node_t));
tmp->num = num;
tmp->next = NULL;
head = tail = tmp;
} else {
test_node_t *tmp_add = (test_node_t *)malloc(sizeof(test_node_t));
tmp_add->num = num;
tmp_add->next = NULL;
tail->next = tmp_add;
tail = tmp_add;
}
}
void print_link()
{
test_node_t *p = head;
for (p = head; p; p = p->next)
printf("the %d node address:%x, head:%x, tail:%x\n", p->num, p, head, tail);
}
void clear_link()
{
test_node_t *p = head;
while (p) {
test_node_t *tmp = p;
p = p->next;
printf("free %d node, tmp:%x, p:%x, head:%x, tail:%x\n", tmp->num, tmp, p, head, tail);
free(tmp);
}
if (p == NULL)
tail = head = NULL;
}
int main(void)
{
long long x = 23LL, y = 0x1229;
char x1, x2;
short a = 0x1122;
int i = 0;
unsigned int j = 18;
for (i = 1; i < 11; i++)
build_link(i);
//print_link();
// clear_link();
printf("head:%x, tail:%x, %d\n", head, tail, i);
x1 = ((char*)&a)[0];
x2 = ((char*)&a)[1];
printf("gxy test:%lld, %d, %u, %x\n", x, i, (i - j), (unsigned int)(-2));
printf("gxy test:long long:%d, long:%d, short:%d, x1:%x, x2:%x\n", sizeof(long long), sizeof(long), sizeof(short), x1, x2);
printf("gxy test:%u, %u, size:%d, ok:%d\n", UNSIGNED_COMP_LT(x, y), UNSIGNED_COMP_LT(y, x),
sizeof(unsigned short), UNSIGNED_SHORT_LT(x, y)? 1: 0);
return 0;
}