C语言中的字符位

字符位: 它是字符中的位数。如今,几乎所有的体系结构都使用8位字节(但情况并非总是如此,一些较旧的机器过去使用7位字节)。可以在

null

让我们看看它的一个应用。假设我们希望打印一个整数的逐字节表示。

例如:

Input  : 4
Output : 00000000 00000000 00000000 00000100

Input  : 12
Output : 00000000 00000000 00000000 00001100 

// CPP program to print byte by byte presentation
#include <bits/stdc++.h>
using namespace std;
// function in which number and intitally 0 is passed
void printInBinary( int num)
{
int n = CHAR_BIT* sizeof (num);
stack< bool > s;
for ( int i=1; i<=n; i++)
{
s.push(num%2);
num = num/2;
}
for ( int i=1; i<=n; i++)
{
cout << s.top();
s.pop();
// Put a space after every byte.
if (i % CHAR_BIT == 0)
cout << " " ;
}
}
int main()
{
int num = 12;
printInBinary(num);
return 0;
}


输出:

00000000 00000000 00000000 00001100 

本文由 阿普瓦·阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享