转换基地10到基地2个,C编程 [英] Convert base 10 to base 2, C programming
问题描述
我还是新编程,我想请问一下 C
语言大约具体问题。我使用$ C $个cblocks编译器。
I'm still new with programming and I'd like to ask about one specific question about the C
language. I'm using codeblocks compiler.
下面是一块code的:
Here is a piece of code:
int n, c, k;
scanf("%d",&n);
for(c = 31;c >= 0;c--)
{
k = n >> c;
if(k & 1)
printf("1");
else
printf("0");
}
return 0;
这是我从一个网站了,它的基地,以二进一转换。
that I got from one website and it converts the base to the binary one.
我的问题是,我在那里有一个,如果和放大器; else语句,为什么会出现,如果(K&安培; 1)的printf(1)?我以为使得k既可以是1和0,如果我使用(K&安培; 1)有两个选项,如(0安培; 1)= 0(1和1)= 1。可以请任何人给我解释一下这个?
非常感谢你。
My question is, where I have an if&else statement, why is there if(k & 1) printf("1") ?? I thought that k can be both 1 and 0 and if I use (k & 1) there are two options such as (0 & 1) = 0 and (1 & 1) = 1. Can please anybody explain me this? Thank you very much.
推荐答案
的内部的前pression如果语句总是判断为真或假。在整数而言,0表示假的,和任何非零意味着真。当你有 K&安培; 1
,这意味着,如果 K
的最低显著位为1,那么前pression计算结果为1,因此被认为真正。如果 K
的最低显著位为0,则前pression计算结果为0,因此被认为是假的。
The expression inside of an if statement always evaluates to true or false. In terms of integers, 0 means false, and anything non-zero means true. When you have k & 1
, that means that if the least-significant bit of k
is 1, then that expression evaluates to 1 and is therefore considered true. If the least-significant bit of k
is 0, then the expression evaluates to 0 and is therefore considered to be false.
这篇关于转换基地10到基地2个,C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!