zoukankan      html  css  js  c++  java
  • 移位实现正负数原码输出

    所谓原码就是二进制定点表示法,即最高位为符号位,“0”表示正,“1”表示负,其余位表示数值的大小。

    反码表示法规定:正数的反码与其原码相同;负数的反码是对其原码逐位取反,但符号位除外。

    补码表示法规定:正数的补码与其原码相同;负数的补码是在其反码的末位加1。

    eg:

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include<stdio.h>  
     3 void to_two(int num);
     4 int main()
     5 {
     6     int a;
     7     scanf("%d", &a);
     8     to_two(a);
     9 
    10     return 0;
    11 }
    12 
    13 void to_two(int num)
    14 {
    15     int cir = 1 << 31;
    16     for (int i = 1; i <= 32; i++)
    17     {
    18         if ((num&cir) == 0)//&的优先级低于==
    19         {
    20             printf("0");
    21         }
    22         else
    23         {
    24             printf("1");
    25         }
    26         num <<= 1;
    27         if (i % 4 == 0)
    28         {
    29             printf(" ");
    30         }
    31     }
    32 }
  • 相关阅读:
    mongodb的索引
    mongodb的简单操作
    mongodb的安装
    redis简单消息队列
    支持utf8的str_split函数
    php curl 传递数据
    linux 安装 ftp
    php des 对称加解密类
    13. Roman to Integer
    12. Integer to Roman
  • 原文地址:https://www.cnblogs.com/yangguang-it/p/6593123.html
Copyright © 2011-2022 走看看