Swap the value of a variable without using an intermediate variable. Find the number of 1 in a binary binary

Source: Internet
Author: User

1. Given the values of the two shaping variables, the contents of the two values are exchanged.
2. Do not allow creation of temporary variables, exchange two number of content (additional questions)
3. Find the maximum value in 10 integers.
4. Write a function to return the number of 1 in the parameter binary
For example: 15 0000 1111 4 x 1
Program prototypes:
int count_one_bits (unsigned int value)
{
Returns the number of digits in 1

}








#include <stdio.h>
#include <stdlib.h>
void Exchange (int *num1, int *num2)
{
int tmp;
TMP = *NUM1;
*NUM1 = *num2;
*NUM2 = tmp;
}
int main ()
{
int num1=0;
int num2=0;
scanf ("%d%d", &num1, &num2);
printf ("num1=%d num2=%d\n", NUM1, num2);
Exchange (&AMP;NUM1,&AMP;NUM2);
printf ("num1=%d num2=%d\n", NUM1, num2);
System ("pause");
return 0;
}






#include <stdio.h>
#include <stdlib.h>
int main ()
{
int num1=0;
int num2=0;
scanf ("%d%d", &num1, &num2);
printf ("num1=%d num2=%d\n", NUM1, num2);
NUM1 = Num1 + num2;
num2 = num1-num2;
NUM1 = num1-num2;
printf ("num1=%d num2=%d\n", NUM1, num2);


System ("pause");
return 0;
}




#include <stdio.h>
#include <stdlib.h>
int max_num (int *p)
{
int i = 0;
for (i = 0; I <9; i++)
{
if (P[i]>p[i + 1])
{
P[i + 1] = P[i];
}
}
return p[9];
}
int main ()
{
int arr[10];
for (int i = 0; i <; i++)
{
scanf ("%d", &arr[i]);
}
int ret = Max_num (arr);
printf ("%d\n", ret);
System ("pause");
return 0;
}






#include <stdio.h>
#include <stdlib.h>
int count_one_bits (unsigned int value)
{
int count = 0;
for (int i = 0; i <; i++)
{
if (value & 1 = = 1)
{
count++;
}
Value = value >> 1;
}
return count;
}
int main ()
{
unsigned int value= 0;
scanf ("%d", &value);
int ret = count_one_bits (value);
printf ("%d\n", ret);
System ("pause");
return 0;
}


This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1751438

Swap the value of a variable without using an intermediate variable. Find the number of 1 in a binary binary

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.