C程序使用按位和移位运算符将十进制转换为二进制 [英] C program to convert decimal to binary using bitwise and, shift operator

查看:502
本文介绍了C程序使用按位和移位运算符将十进制转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码是一本书中的示例,该问题需要使用按位AND oeprator和shift运算符将十进制数更改为二进制数。虽然曾尝试使用调试编译器来理解这段代码,但我无法理解代码。假设a和b,用户输入为10和8 





 #include< stdio.h> 
#include< stdlib.h>


int count_bits(unsigned x)
{

int bits = 0;
while(x){
if(x& 1U)bits ++;
x>> = 1;


}返回位;
}

int int_bits(void)
{
return count_bits(~0U);
}

void print_bits(unsigned x) {
int i;

for(i = int_bits(x)-1; i> ; = 0; i--)
putchar(((x>> i)& 1U)?'1':'0');
}
int main(void)
{
unsigned a,b; / *假设用户输入a = 10 b = 8 * /
printf(输入两个正整数值= \ n);
printf(a =);的scanf( %u 的,&安培;一个);
printf(b:);的scanf( %u 的,和b);

printf(\ na =); print_bits(一);
printf(\ na =); print_bits(b)中;
返回0;
}





我的尝试:


实际上是什么(~0U)呢? <预> 
count_bits(unsigned x)函数中x的值是多少?和(x& 1U)中的比较是什么,count_bits(~0U)与用户输入的关系是什么? putchar中的
in print_bits函数(((x>> i)& 1u)?'1';'0'); x和i的价值是多少?
i得到i = 32作为来自count_bits的位



这个程序实际上产生二进制数的是什么?

解决方案

  int  int_bits( unsigned  x)
{
return count_bits(~0U);
}



看起来不对,因为它会为 x 的任何值返回相同的值。我建议将其更改为:

  return  count_bits(x); 


Quote:

C程序将十进制转换为二进制使用按位和移位运算符



程序不转换,它只显示基数2中的值。

注意,在内部,整数存储在C程序中的二进制文件。



要知道什么是& ,只需阅读有关按位运算符的文档。

要了解什么是 U ,请阅读有关整数常量的文档。

引用:

我无法理解代码虽然曾尝试使用调试编译器来理解这段代码。



您可以通过在机器语言级别使用调试器来更好地理解源代码的翻译方式。


this code is an example from a book that the problem require to change decimal to binary number using bitwise AND oeprator and shift operator. i cannot understand the code although had tried to understand this code using debug compiler. suppose for a and b, user input is 10 and 8



#include <stdio.h>
#include <stdlib.h>


int count_bits (unsigned x)
{

    int bits=0;
    while(x){
        if (x&1U)bits++;
            x>>=1;


    } return bits;
}

int int_bits(void)
{
    return count_bits(~0U);
}

void print_bits(unsigned x){
    int i;
    for(i=int_bits(x)-1;i>=0;i--)
        putchar(((x>>i)&1U)?'1':'0');
}
int main(void)
{
 unsigned a,b; /*suppose user input a=10 b=8*/
 printf("enter two positive integer value=\n");
 printf("a=  "); scanf("%u",&a);
 printf("b:  "); scanf("%u",&b);

 printf("\na   =");  print_bits(a);
 printf("\na   =");  print_bits(b);
    return 0;
}



What I have tried:

in int_bits function what actually (~0U) does? <pre>
 what is the value of x in count_bits(unsigned x) function? and what is being compare in (x & 1U) and what is the relation of count_bits(~0U) and user input?
in print_bits function in putchar (((x>>i)&1u)?'1';'0'); what is the value of x and i? 
i got i=32 as bits from count_bits


what actually this program do to produce the binary number?

解决方案

int int_bits(unsigned x)
{
    return count_bits(~0U);
}


That looks wrong, since it will return the same value for any value of x . I suggest changing it to:

return count_bits(x);


Quote:

C program to convert decimal to binary using bitwise and, shift operator


The program do not convert, it just display the value in base 2.
Note that internally, integers are stored in binary in C programs.

To know what are & and ~, just read the documentation about 'bitwise operators'.
To know what is U, read documentation about 'integer constant'.

Quote:

i cannot understand the code although had tried to understand this code using debug compiler.


You will better understand by using the debugger at machine language level to see exactly how source code is translated.


这篇关于C程序使用按位和移位运算符将十进制转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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