将基数10转换为基数2,C编程

将基数10转换为基数2,C编程,c,binary,codeblocks,C,Binary,Codeblocks,我还是编程新手,我想问一个关于C语言的具体问题。我正在使用代码块编译器 下面是一段代码: 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; 我从一个网站上得到的,它将基数转换成二进制 我的问题是,如果我有if&e

我还是编程新手,我想问一个关于
C
语言的具体问题。我正在使用代码块编译器

下面是一段代码:

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;
我从一个网站上得到的,它将基数转换成二进制

我的问题是,如果我有if&else语句,为什么会有if(k&1)printf(“1”)??我认为k可以是1和0,如果我使用(k&1),有两个选项,例如(0&1)=0和(1&1)=1。谁能给我解释一下吗?
非常感谢。

if语句中的表达式的计算结果始终为true或false。就整数而言,0表示假,任何非零表示真。当您有
k&1
时,这意味着如果
k
的最低有效位为1,则该表达式的计算结果为1,因此被认为是真的。如果
k
的最低有效位为0,则表达式的计算结果为0,因此被视为false。

If语句中的表达式的计算结果始终为true或false。就整数而言,0表示假,任何非零表示真。当您有
k&1
时,这意味着如果
k
的最低有效位为1,则该表达式的计算结果为1,因此被认为是真的。如果
k
的最低有效位为0,则表达式的计算结果为0,因此被认为是错误的。

当我开始时,有一件事帮助了我,那就是用笔和纸“手动”运行代码几次

给你一个例子,让我们考虑你的循环(但是使用8位整数而不是32位整数):

我假设您的示例中的其他代码不变。此外,假设用户输入218(或二进制输入11010)

第一次迭代:

n is 218, c is 7.
we do a right shift 7 (n >> c) which makes k 00000001
we do a bit-wise `and' of k and 1 (K & 1), which is 1 or true
we print "1"
n is 218, c is 6.
we do a right shift 6 (n >> c) which makes k 00000011
we do a bit-wise `and' of k and 1 (k & 1), which is 1 or true
we print "1"
n is 218, c is 5.
we do a right shift 5 (n >> c) which makes k = 00000110
we do a bit-wise `and' of k and 1 (k&1), which is 0 or false
we print "0"
第二次迭代:

n is 218, c is 7.
we do a right shift 7 (n >> c) which makes k 00000001
we do a bit-wise `and' of k and 1 (K & 1), which is 1 or true
we print "1"
n is 218, c is 6.
we do a right shift 6 (n >> c) which makes k 00000011
we do a bit-wise `and' of k and 1 (k & 1), which is 1 or true
we print "1"
n is 218, c is 5.
we do a right shift 5 (n >> c) which makes k = 00000110
we do a bit-wise `and' of k and 1 (k&1), which is 0 or false
we print "0"
第三次迭代:

n is 218, c is 7.
we do a right shift 7 (n >> c) which makes k 00000001
we do a bit-wise `and' of k and 1 (K & 1), which is 1 or true
we print "1"
n is 218, c is 6.
we do a right shift 6 (n >> c) which makes k 00000011
we do a bit-wise `and' of k and 1 (k & 1), which is 1 or true
we print "1"
n is 218, c is 5.
we do a right shift 5 (n >> c) which makes k = 00000110
we do a bit-wise `and' of k and 1 (k&1), which is 0 or false
we print "0"
等等

因此,k可以是0或1,这是正确的,但是它在循环中每次仅假定其中一个值

最后,如果您不清楚按位“and”如何工作,它会查看两个数字中的第n位,如果它们相同,则将结果的第n位设置为1,否则将结果设置为0。考虑218位和15位:

218  is 11011010     e.g. third bit from right is zero
 15  is 00001111     e.g. third bit from right is one
-----------------
  &     00001010     e.g. third bit from right is zero

在我刚开始的时候,有一件事帮助了我,那就是用笔和纸“手动”运行代码几次

给你一个例子,让我们考虑你的循环(但是使用8位整数而不是32位整数):

我假设您的示例中的其他代码不变。此外,假设用户输入218(或二进制输入11010)

第一次迭代:

n is 218, c is 7.
we do a right shift 7 (n >> c) which makes k 00000001
we do a bit-wise `and' of k and 1 (K & 1), which is 1 or true
we print "1"
n is 218, c is 6.
we do a right shift 6 (n >> c) which makes k 00000011
we do a bit-wise `and' of k and 1 (k & 1), which is 1 or true
we print "1"
n is 218, c is 5.
we do a right shift 5 (n >> c) which makes k = 00000110
we do a bit-wise `and' of k and 1 (k&1), which is 0 or false
we print "0"
第二次迭代:

n is 218, c is 7.
we do a right shift 7 (n >> c) which makes k 00000001
we do a bit-wise `and' of k and 1 (K & 1), which is 1 or true
we print "1"
n is 218, c is 6.
we do a right shift 6 (n >> c) which makes k 00000011
we do a bit-wise `and' of k and 1 (k & 1), which is 1 or true
we print "1"
n is 218, c is 5.
we do a right shift 5 (n >> c) which makes k = 00000110
we do a bit-wise `and' of k and 1 (k&1), which is 0 or false
we print "0"
第三次迭代:

n is 218, c is 7.
we do a right shift 7 (n >> c) which makes k 00000001
we do a bit-wise `and' of k and 1 (K & 1), which is 1 or true
we print "1"
n is 218, c is 6.
we do a right shift 6 (n >> c) which makes k 00000011
we do a bit-wise `and' of k and 1 (k & 1), which is 1 or true
we print "1"
n is 218, c is 5.
we do a right shift 5 (n >> c) which makes k = 00000110
we do a bit-wise `and' of k and 1 (k&1), which is 0 or false
we print "0"
等等

因此,k可以是0或1,这是正确的,但是它在循环中每次仅假定其中一个值

最后,如果您不清楚按位“and”如何工作,它会查看两个数字中的第n位,如果它们相同,则将结果的第n位设置为1,否则将结果设置为0。考虑218位和15位:

218  is 11011010     e.g. third bit from right is zero
 15  is 00001111     e.g. third bit from right is one
-----------------
  &     00001010     e.g. third bit from right is zero

这只是
if((k&1)!=0)
的简写;然后尝试0而不是123。@OliCharlesworth值得注意的是,OP的if语句并不是您所描述内容的真正简写。这两个语句当然是等价的,但是你所说的是等价于说
if(true)
if(true!=0)
的简写,虽然这两个语句是等价的,但前者并不是后者的简写。语句
if(true)
不是为上面的后一个语句创建的速记(顺便说一下,该分支可以被更紧凑、无分支的
putchar('0'+(k&1));
)@Wolfram:是的,这是真的。它只是
if((k&1)!=0)
的速记。试试看:if(123)printf(“Yes”);然后尝试0而不是123。@OliCharlesworth值得注意的是,OP的if语句并不是您所描述内容的真正简写。这两个语句当然是等价的,但是你所说的是等价于说
if(true)
if(true!=0)
的简写,虽然这两个语句是等价的,但前者并不是后者的简写。语句
if(true)
不是作为上述后一个语句的简写而创建的。(顺便说一句,该分支可以被更紧凑、无分支的
putchar('0'+(k&1));
)@Wolfram:是的,这是真的。+1很好的答案:注意:不变的“first”,如“k的第一位”中的“first”表示一些歧义。建议“k的最低有效位”+1回答很好:注意:不变的“first”,如“k的第一位”,表示一些歧义。建议“k的最低有效位”。