首页 > 其他 > 详细

无符号类型赋值负数,以及求绝对值

时间:2016-11-26 02:40:39      阅读:254      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <cmath>

int main()
{
    unsigned char i = 0 ;

    i = -1 ;
    std::cout<<"i="<<(int)i<<std::endl;

    i = -2 ;
    std::cout<<"i="<<(int)i<<std::endl;

    i = -3 ;
    std::cout<<"i="<<(int)i<<std::endl;

    i = 256 ;
    std::cout<<"i="<<(int)i<<std::endl;

    i = 257 ;
    std::cout<<"i="<<(int)i<<std::endl;

    i = 258 ;
    std::cout<<"i="<<(int)i<<std::endl;

    std::cout<<"---------------------------"<<std::endl;

    std::cout<<(int)std::abs(-100)<<std::endl;

    std::cout<<std::abs(-100.0)<<std::endl;

    std::cout<<std::abs(-100.0D)<<std::endl;

    return 0;
}

 具体原理涉及原码->反码->补码

技术分享

 

求绝对值函数abs()有几个重载,传入int,返回值不是int,需要强制转换

技术分享

 

无符号类型赋值负数,以及求绝对值

原文:http://www.cnblogs.com/guozhikai/p/6103441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!