Simple Application of shift and logical operators in C Language

Source: Internet
Author: User

Simple Application of shift and logical operators in C Language
& Operator 1: calculate the number of 1 in binary. For example, 15 0000 1111 4 1 requires that the number be stored in int count_one_bits (unsigned int value) // calculate the number of digits of 1 function {int count; count = 0; while (value) {count ++; value = value & (value-1); // each & every time, reduce the number of "1" in a binary number;} return count ;}& operator and> operator Union Application 2. obtain all the even and odd digits in a binary sequence, outputs binary sequences respectively. # Include <stdio. h> # include <stdlib. h> int main () {int I, j; int num; // defines the input variables int a [16], B [16]; // define the printf array for storing odd and even digits ("enter a decimal number:"); scanf ("% d", & num); for (I = 0, j = 0; j <32; I ++, j = j + 2) // obtain the odd number of the binary series {a [15-i] = (num> j) & 1;} printf ("from high to low odd digits:"); for (I = 0; I <16; I ++) // output the binary odd digits from the high position to the low position {printf ("% d", a [I]);} for (I = 0, j = 1; j <32; I ++, j = j + 2) // obtain the even digits of the binary sequence {B [15-i] = (num> j) & 1 ;} printf ("\ n from high to low even bits are:"); for (I = 0; I <16; I ++) // output the even binary digits from the high position to the low position {printf ("% d", B [I]);} system ("pause"); return 0 ;} | Operator 3: Determine whether the ranking is consecutive/* match the ranking to the binary digit in a certain number */

Int ret = 0, flag = 0; ret | = (1 <(a-1); ret | = (1 <(B-1 )); ret | = (1 <(c-1); ret | = (1 <(d-1 )); ret | = (1 <(e-1); while (ret) {if (0 = ret % 2) {flag = 1;} ret/= 2 ;} if (0 = flag) {printf ("rankings: \ n "); printf ("a = % d \ nb = % d \ nc = % d \ nd = % d \ ne = % d \ n", a, B, c, d, e);} 4. How many digits (bit) are different int different_bits (int num_1, int num_2) in the Binary Expression of two int (32-bit) integers m and n) {int count = 0; for (int I = 0; I <32; I ++) {if (num_1> I) & 1) ^ (num_2> I) & 1) {count ++ ;}} return (count );}

 

5. unsigned int reverse_bit (unsigned int num) {int I = 0; unsigned int ret = 0; for (I = 0; I <32; I ++) {ret <= 1; ret | = (num> I) & 1) ;}return (ret );} 6. Calculate the average value of two numbers without Overflow int average (int a, int B) {return (a & B) + (a ^ B)> 1);} 7. Find the int find_only (int arr [], int size) {for (int I = 1; I <size; I ++) {arr [0] ^ = arr [I];} return (arr [0]);}

Related Article

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.