1. 十进制二进制转换
#include <iostream>
using namespace std;
int main()
{
int a[10], n, i;
cout<<"Enter the number to convert: ";
cin>>n;
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)
{
cout<<a[i];
}
return 0;
}
输出
Enter the number to convert: 9
Binary of the given number= 1001
2.进制左移右移例子
#include <iostream>
using namespace std;
int A, B, cnt0, cnt1;
int main() {
int n, t, temp;
cin >> n;
for (int i = 1; i <= n; i++) {
cnt0 = 0, cnt1 = 0;
t = i;
while (t) {
temp = t & 1;
t = t >> 1;
if (temp) cnt1++