Programming questions related to integer binary sequences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.