如何打印二进制

wangjk_wj 2008-04-12 04:24:31

C 如何把一个整数以二进制形式打印出来,例如:9 输出结果为:1001b
...全文
4190 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcu5555 2012-09-03
  • 打赏
  • 举报
回复
见识了比较牛的人,我等菜鸟拜服。
AndyZhang 2011-08-04
  • 打赏
  • 举报
回复
%b 不行么,不行的话,就mod2 求出所有二进制,输出
AnYidan 2011-08-04
  • 打赏
  • 举报
回复

void Convert(int n, unsigned int radix)
{
if (n < 0 && radix == 10)
{
putchar('-');
n = -n;
}

n = (unsigned int)n;

if (n / radix)
{
Convert(n / radix, radix);
}

if (n % radix > 9)
putchar(n % radix - 10 + 'a');
else
putchar(n % radix + '0');


}

int main()
{
int n;
scanf("%d", &n);
Convert(n, 2);
printf("\n");
system("pause");
return 0;
}
Jinn 2011-08-04
  • 打赏
  • 举报
回复
so good.
DIE654456 2011-08-04
  • 打赏
  • 举报
回复

windows下的
<stdlib.h>
char *_itoa( int value, char *string, int radix );

int i = 3445;
char buffer[20];
_itoa( i, buffer, 2 );

lvzehong 2008-04-18
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <string.h>

int fun(int n,char *str)
{
int i=0;
while(n)
{
*(str+i)=n%2+'0';
i++;
n=n/2;
}
*(str+i)='\0';
return 0;
}

main()
{
int data,j,len;
char string[100],temp;
cout<<"input one data:";
cin>>data;
fun(data,string);
len=strlen(string);
for(j=0;j<=len/2;j++)
{
temp=*(string+j);
*(string+j)=*(string+len-1-j);
*(string+len-1-j)=temp;
}
cout<<string<<'b'<<endl;
return 0;
}
hanb99 2008-04-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

int main()
{
int a = 1235;
int i;
unsigned int u = 1, b;
u = u << sizeof(int) * 8 - 1;
for (i = 0; i < sizeof (int) * 8; ++i)
{
b = (a << i) & u;
printf("%d", b >> sizeof(int) * 8 - 1);
}
printf("\n");
}
wangjk_wj 2008-04-18
  • 打赏
  • 举报
回复
非常感谢.希望大家以后多多帮忙.
pp77xf 2008-04-18
  • 打赏
  • 举报
回复
学习了
kntism 2008-04-18
  • 打赏
  • 举报
回复
喜欢简单点的
void binary_(int x)
{
if (x<=0) return;
else
{
binary_(x/2);
printf("%d",x%2);
}
}
ForestDB 2008-04-18
  • 打赏
  • 举报
回复
#include <stdio.h>

typedef unsigned char BYTE;

void print_binary(BYTE byte)
{
int i;
for(i = 7; i >= 0; --i)
{
printf("%d", (byte >> i) & 0x01);
}
}

int main(void)
{
BYTE a = 0xAA;
print_binary(a);
putchar('\n');
return 0;
}


// under windows, you can also try itao()
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char buffer[33];
int a = 0x55555555;

itoa(a, buffer, 2);
printf("%s\n", buffer);

return 0;
}
ChamPagneZ 2008-04-12
  • 打赏
  • 举报
回复
唯一想起来的是递归
lixun_21 2008-04-12
  • 打赏
  • 举报
回复
#include <stdio.h>
void main()
{
int num,i;
unsigned char flag;
scanf("%d",&num);
for(i=0,flag=0x80;i<8;i++,flag/=2)
{
if(num&flag)
printf("1");
else if(num/flag)
printf("0");
}
printf("b\n");
}

^_^
wangxipu 2008-04-12
  • 打赏
  • 举报
回复
ls的几位都很强大

也可以用递归呀

void binary_(int x)
{
if (x<=0) return;
else
{
binary_(x/2);
printf("%d",x%2);
}
}
vagrantfish 2008-04-12
  • 打赏
  • 举报
回复

int main()
{
int n, i;
unsigned mask;

scanf("%d", &n);

mask = 1 << (sizeof(n)*8 - 1);
for (i = 0; i < sizeof(n)*8; i++)
{
if (n & mask)
putchar('1');
else
putchar('0');
n <<= 1;
}
}


正负数都可以打印
clhposs 2008-04-12
  • 打赏
  • 举报
回复

#include<iostream>
#include<bitset>

using namespace std;

void two(int x)
{
bitset<32> two(x);
for(int len=two.size()-1;len>=0;--len)
cout<<two[len];
cout<<endl;
}
int main()
{
int two_to;
cout<<"Please input:"<<endl;
cin>>two_to;
two(two_to);
return 0;

}
a176835359 2008-04-12
  • 打赏
  • 举报
回复
#include<stdio.h>
void main()
{
int bin[20];//二进制长度
int i, integer;
scanf("%d", &integer);//输入的整数
/*
* 把integer中的低位存到数组bin的高位
* 如9对应的二进制数是1001,bin[19]=1,bin[18]=0,bin[17]=0,bin[17]=1
*/
for(i = 19; integer != 0; i--)
{
bin[i] = integer % 2;
integer = integer / 2;
}
//由于上面计算完成后i值减1,因此在初始化要先将i值加1
for(i++; i < 20; i++)
printf("%d", bin[i]);
printf("b\n");
}
Inhibitory 2008-04-12
  • 打赏
  • 举报
回复
#include <iostream>
#include <bitset>
#include <string>

void printBinary(int n) {
std::bitset<32> bits(n);
for (int i = bits.size() - 1; i >= 0; --i) {
std::cout << bits[i];
}
std::cout << std::endl;
}

int main() {
printBinary(10);
printBinary(-10);
printBinary(23);

return 0;
}


嘿嘿, 简单吧.
最大可能的利用 STL
ttkk_2007 2008-04-12
  • 打赏
  • 举报
回复

void fun(int i, char *res){
int j = 0;
while(i){
*(res + j) = i % 2 + '0';
i /= 2;
++j;
}
*(res + j) = '\0';
strrev(res);
}


int main(){
char *p = (char*)malloc(20);
int i = 35;
fun(i, p);
cout << p << 'b' <<endl;
}
Supper_Jerry 2008-04-12
  • 打赏
  • 举报
回复
转换成二进制字符串

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧