Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2970106
  • 博文数量: 523
  • 博客积分: 11908
  • 博客等级: 上将
  • 技术积分: 5475
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-03 15:50
文章分类

全部博文(523)

文章存档

2019年(3)

2013年(4)

2012年(71)

2011年(78)

2010年(57)

2009年(310)

分类: C/C++

2009-06-20 21:45:00

首先进行一个实验,分别定义一个signed int型数据和unsigned int型数据,然后进行大小比较:
    unsigned int a=20;
    signed int b=-130;
a>b?还是b>a?实验证明b>a,也就是说-130>20,为什么会出现这样的结果呢?
这是因为在C语言操作中,如果遇到无符号数与有符号数之间的操作,编译器会自动转化为无符号数来进行处理,因此a=20,b=4294967166,这样比较下去当然b>a了。
再举一个例子:
 unsigned int a=20;
 signed int b=-130;
 std::cout<结果输出为4294967186,同样的道理,在运算之前,a=20,b被转化为4294967166,所以a+b=4294967186

减法和乘法的运算结果类似。

如果作为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;
}

阅读(1690) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~