C语言巧妙运用位运算十进制转化为二进制输出

来源:互联网 发布:java登录界面设计代码 编辑:程序博客网 时间:2024/06/04 18:31

巧妙运用位运算十进制转化为二进制输出,正数负数都适合

代码:

#include<stdio.h>#include <stdlib.h>#include <string.h>int main(){    int numb;    char *buff = (char*)malloc(10*sizeof(char));    int count = 0;    scanf("%d",&numb);    count = 0;    int index = 0;    do{        buff[count++] = numb&1;        numb = numb >> 1;        if(numb == 0){            break;        }        index ++;    }while(index < 32);    for(count--;count >= 0; count--){        printf("%d",buff[count]);    }    free(buff);    return 0;}

输出结果
输入-2

-211111111111111111111111111111110

输入15

1710001
0 0
原创粉丝点击