二进制数 [英] Binary number

查看:93
本文介绍了二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C中以二进制格式显示整数?


例如4显示为100

How do I display an integer in binary format in C?

e.g. 4 displayed as "100"

推荐答案

Davey写道:

如何显示整数C中的二进制格式?

4显示为100

How do I display an integer in binary format in C?

e.g. 4 displayed as "100"




这是一种常用的方法:


1)足够预留存储一个字符串,可以包含整个

的结果。

如果你的整数是一个unsigned long int,那么你知道它是

不能超过sizeof(long int)* CHAR_BIT值位,所以只需

定义一个char数组,sizeof(long int)* CHAR_BIT + 1个字节

长度。


2)指向字符串的开头。


3)如果数字是偶数,写'' 0''通过指针。否则,

通过指针写'1'。


4)递增指针。


5)将数字除以2.


6)如果数字不为零,则继续执行步骤3)。


7)通过指针写''\ 0'',以空字符串终止字符串。


8)反转字符串,注意将终结符留在原位。



Here''s one common way to do it:

1) reserve enough storage for a string that can contain the whole
result.
If your integer is an unsigned long int, say, then you know that it
can''t have more than sizeof(long int) * CHAR_BIT value bits, so just
define an array of char, sizeof(long int) * CHAR_BIT + 1 bytes in
length.

2) point to the start of the string.

3) if the number is even, write ''0'' through the pointer. Otherwise,
write ''1'' through the pointer.

4) increment the pointer.

5) divide the number by 2.

6) if the number is non-zero, continue from step 3).

7) write ''\0'' through the pointer, to null-terminate the string.

8) reverse the string, taking care to leave the terminator in place.


Davey写道:
如何在C中以二进制格式显示整数?

例如4显示为100
How do I display an integer in binary format in C?

e.g. 4 displayed as "100"



你应该自己试试。


void bits(uchar b,int n) {

for(--n; n> = 0; --n)

putchar((b& 1<< n)?''1 '':''0'');

putchar('''');

}


以上是不是节目,而是线索。

-

Joe Wright mailto:jo ******** @ comcast.net

所有事情都应尽可能简单,但并不简单。

---阿尔伯特爱因斯坦---


You should try it yourself.

void bits(uchar b, int n) {
for (--n; n >= 0; --n)
putchar((b & 1 << n) ? ''1'' : ''0'');
putchar('' '');
}

The above is not a program, but a clue.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---




Davey写道:

Davey wrote:
如何在C中以二进制格式显示整数?

例如4显示为100
How do I display an integer in binary format in C?

e.g. 4 displayed as "100"



#include< stdio.h>

#include< limits.h>

#define SHOWBITS(var)bitshow((unsigned char *)& var,sizeof(var))

int bitshow(unsigned char * p,int size)

{

int count = 0;

int byte;

while(size)

{

byte = CHAR_BIT;

while(byte> 0)

{

if( * p& 1<<< byte)

{

putchar(''1'');

count ++;

}

其他putchar(''0'');


字节 - ;

} < br $> b $ b putchar(''\ n'');

size--;

p ++;

}

返回计数;

}


int main(无效)

{

int i = 0xFD1;


SHOWBIT(i); / *返回设置位数,丢弃* /


puts(完成时按Enter键),getchar();

返回0;

}


#include <stdio.h>
#include <limits.h>

#define SHOWBITS(var) bitshow((unsigned char *)&var, sizeof(var))

int bitshow(unsigned char *p, int size)
{
int count = 0;
int byte;
while(size)
{
byte = CHAR_BIT;
while(byte > 0)
{
if(*p & 1 << byte)
{
putchar(''1'');
count++;
}
else putchar(''0'');

byte--;
}
putchar(''\n'');
size--;
p++;
}
return count;
}

int main(void)
{
int i= 0xFD1;

SHOWBIT(i); /* returns number of set bits, discarded */

puts("Press ENTER when done"), getchar();
return 0;
}


这篇关于二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆