1. get all the even and odd digits in a binary sequence and output the binary sequence separately
#include <stdio.h>
int main ()
{
Inti,j,n,m;
CHARA[32]={0};
printf ("Peleaseinput the Number:");
scanf ("%d", &n);
for (m=0;m<32;m++)
{
I= (unsignedint) n%2;
j= (unsignedint) N/2;
N=j;
A[m]=i;
}
printf (" This number of binary sequence is : \ n");
for (m=31;m>=0;m--)
{
printf ("%d", a[m]);
}
printf ("\ n binary odd digit sequence is : \ n");
for (m=31;m>=0;m=m-2)
{
printf ("%d", a[m]);
}
printf ("\ n binary even digit sequence : \ n");
for (m=30;m>=0;m=m-2)
{
printf ("%d", a[m]);
}
printf ("\ n");
Rwturn0;
}
2. Write a function to return the number of arguments in binary one
#include <stdio.h>
int count_one_bits (unsigned int value)
{
Unsignedint bitnum=0;
while (value>0)
{
Value&= (value-1);
bitnum++;
}
Returnbitnum;
}
int main ()
{
Ints,value;
Intcount_one_bits (unsigned int value);
scanf ("%d", &value);
S=count_one_bits (value);
printf ("%d\n", s);
}
Note: The formula value&= (value-1) in the case of Value>0 not every & will be bits in a 1 to 0,value to 0, the number of times & is the number of binary.
3. Find out how many bits are different in the two unsigned number bits.
Train of thought: The question uses m&= (m-1) to seek the number of the bits in 1, and two number is different or operation, the difference is 1, so the number of the difference or the result of 1 is two number of bits in the number of different bits.
#include <stdio.h>
#include <stdlib.h>
int count_one_bits (unsigned int value)
{
unsigned int bitnum = 0;
while (value>0)
{
Value &= (value-1);
bitnum++;
}
return bitnum;
}
int count_differbit (unsigned int m, unsigned int n)
{
unsigned int ret;
unsigned int A;
A = m ^ n;
ret = Count_one_bits (a);
return ret;
}
int main ()
{
int C, D;
int result;
printf ("Please enter two integers: \ n");
scanf_s ("%d%d", &c, &d);
Result=count_differbit (c, D);
printf ("Two number of binary expressions with%d different!") ", result);
System ("pause");
return 0;
}
Programming questions related to integer binary sequences