C language as arithmetic and assignment problem of two variables

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators

#include <stdio.h>
#define ARRAY_SIZE 10
int main ()
{
int Arr[array_size] = {51,116,53,120,85,66,71,98,86,100};
int I, J;
for (i = 0; i < array_size; i++)
{
for (j = 0; J < Array_size-1; J + +)
if (Arr[j] > arr[j+1]) {
ARR[J] ^= arr[j+1];
ARR[J+1] ^= arr[j];
ARR[J] ^= arr[j+1];
}
}
for (i = 0; i < array_size; i++)
printf ("%c", Arr[i],arr[i]);
}

Run the result is???????

Language bitwise operators: With, OR, XOR, reverse, move left, and right

A bitwise operation is a binary-based operation. In system software, it is often necessary to deal with bits problems. The C language provides 6 bit manipulation operators. These operators can only be used for integer operands, that is, only for signed or unsigned char,short,int and long types.

list of bit operators provided by the C language:
operator meaning description
& Bitwise AND if two corresponding bits are 1, then the result value of this bit is 1, otherwise 0
| In the bitwise or two corresponding bits, as long as there is a value of 1, the result of that bit is 1.
^ Bitwise XOR or if the two bits value of the participating operation is the same as 0, otherwise 1
~ Inverse ~ is a unary operator, used to reverse a binary number bitwise, will be 0 to 1, 1 to 0
<< left shifts all bits of a number to the left n bits, right 0
>> right shifts the bits of a number to the right by n bits, to the right end of the low is discarded, for unsigned number, high 0


1, bitwise-AND Operator (&)
    bitwise-and refers to: two data to participate in the operation, and the "and" operation by bits. If two corresponding bits are 1, the result value of the bit is 1; otherwise 0. The 1 here can be understood as the logic in which the true,0 can be interpreted as false in logic. Bitwise AND is actually the same as the logical "and" arithmetic rules. The logic of "and", the demand for the operation of the whole truth, the result is true. If, a=true,b=true, A∩b=true for example: 3&5 3 is a binary code of 11 (2). (in order to differentiate between decimal and other binaries, this article stipulates that all non-decimal data are appended with parentheses after the data, annotated with the binary in parentheses, and binary labeled as 2). The basic unit of memory storage data is byte (byte), and a byte consists of 8 bits (bit). Bits are the smallest unit used to describe the amount of computer data. In a binary system, each 0 or 1 is a bit. 11 (2) is made up to one byte, then 00000011 (2). The binary encoding of 5 is 101 (2) and is 00000101 (2)
Bitwise AND Operation:
00000011 (2)
&00000101 (2)
00000001 (2)
3 &5=1
C language code:
#include <stdio.h>
Main ()
{
int a=3;
int b = 5;
printf ("%d", a&b);
}
Bitwise AND Uses:
(1) Clear 0
If you want to clear a storage unit, even if all of its bits is 0, just look for a binary number, where each bit meets the criteria:

The original number is 1 bits, and the corresponding bit in the new number is 0. Then make the two & operations, you can achieve the goal of clear 0.
Example: The original number is 43, that is 00101011 (2), another number, set it to 148, that is, 10010100 (2), the two bitwise AND operation:
00101011 (2)
&10010100 (2)
00000000 (2)
C Language Source code:
#include <stdio.h>
Main ()
{
int a=43;
int B = 148;
printf ("%d", a&b);
}
(2) Take some of the points in a number to locate
If you have an integer a (2byte), you want to take the low byte, you only need to put a and 8 1 bitwise with.
A 00101100 10101100
B 00000000 11111111
C 00000000 10101100
(3) retention refers to positioning:
With a number of "bitwise-and" operations, this number takes 1 in that bit.
For example, there is a number 84, or 01010100 (2), which would like to keep the 3,4,5,7,8 bit from the left, the operation is as follows:
01010100 (2)
&00111011 (2)
00010000 (2)
namely: a=84,b=59
C=a&b=16
C Language Source code:
#include <stdio.h>
Main ()
{
int a=84;
int B = 59;
printf ("%d", a&b);
}


2, bitwise OR operator (|)
Two corresponding bits as long as there is a value of 1, the result of this bit is 1. In the case of logic or arithmetic, one is true.


For example: 60 (8) |17 (8), octal 60 and octal 17 are bitwise OR operations.
00110000
|00001111
00111111
C Language Source code:
#include <stdio.h>
Main ()
{
int a=060;
int b = 017;
printf ("%d", a|b);
}
Application: Bitwise OR operation is commonly used to set a value of 1 for a certain bit of data. For example, if you want to change the lower 4 bits of a number A to 1, you only need to make a and 17 (8) bitwise OR operations.


3. Exchange two values without a temporary variable
For example: A=3, i.e. one (2); b=4, i.e. 100 (2).
To swap the values of a and B, you can use the following assignment statements:
A=a∧b;
B=b∧a;
A=a∧b;
a=011 (2)
(∧) b=100 (2)
a=111 (2) (the result of A∧b, A has become 7)
(∧) b=100 (2)
b=011 (2) (the result of B∧a, B has become 3)
(∧) a=111 (2)


A=100 (2) (the result of A∧b, A has become 4)
is equivalent to the following two steps:
① executes the first two assignment statements: "A=A∧B;" and "b=b∧a;" equivalent to B=b∧ (A∧B).
② executes a third assignment statement: A=a∧b. Since the value of a is equal to (A∧B), the value of B is equal to (B∧A∧B),

Thus, the equivalent of A=a∧b∧b∧a∧b, that is, the value of a equals a∧a∧b∧b∧b, equals B.
It's amazing!
C Language Source code:
#include <stdio.h>
Main ()
{
int a=3;
int b = 4;
A=a^b;
B=b^a;
A=a^b;
printf ("a=%d b=%d", A, b);
}


4, "Inverse" operator (~)
He is a unary operator, which is used to find the binary inverse of integers, that is, 1 of each bits on the operand is changed to 0, and 0 becomes 1.
Example: ~77 (8)
Source:
#include <stdio.h>
Main ()
{
int a=077;
printf ("%d", ~a);
}


5. Left shift operator (<<)

The left shift operator is used to shift the bits of a number to the left by several bits, the number of bits moved is specified by the right operand (the right operand must be a non-negative

Value), the right-hand vacated bits are filled with 0, and the high-left-shift overflow is discarded.
For example: The binary number of a is shifted to the left 2 bits, the right side vacated the bit 0, left overflow bit discarded. If a=15, i.e. 00001111 (2), shift left 2

The position was 00111100 (2).
Source:
#include <stdio.h>
Main ()
{
int a=15;
printf ("%d", a<<2);
}
Shift left 1 is equal to the number multiplied by 2, the left Shift 2 is equal to the number multiplied by 2*2=4,15<<2=60, that is, by 4. However, this conclusion applies only to the

A number that is discarded when the left shift is dropped does not contain a 1 case.
Suppose that an integer is stored in one byte (8 bits), and if A is an unsigned integer variable, then when a=64, the left one is overrun by 0.

, while moving the left 2 bits, the overflow high has 1.


6. Right shift operator (>>)
The right-shift operator is used to move the bits of a number to the right of several bits, the number of bits moved is specified by the right operand (the right operand must be a non-negative

Value), move to the right end of the low is discarded, for unsigned number, high 0. For signed numbers, some machines will be left blank

Fill with the sign bit (i.e. "arithmetic shift"), while others fill the left empty part with 0 (i.e. "logical shift"). Note

Meaning: For unsigned numbers, the left-hand side moves to 0 when you move right, and if the original sign bit is 0 (positive) for signed values, the left side is also shifted

Into the 0. If the sign bit turns out to be 1 (that is, negative), the left-hand side is 0 or 1, depending on the computer system used. Some systems move into 0, some

The system moves into 1. Moving into 0 is called "logical shift", i.e. simple shift, and moving into 1 is called "Arithmetic shift".
Example: The value of a is octal number 113755:
a:1001011111101101 (in binary form)
A>>1:0100101111110110 (logical right Shift)
a>>1:1100101111110110 (when arithmetic right shifts)
In some systems, A>>1 has an octal number of 045766, while on some other systems it is possible to get 145766. Turbo C and some other C

The compilation takes the arithmetic right shift, that is, when the signed number is shifted to the right, if the sign bit is 1, the left side of the high is 1.
Source:
#include <stdio.h>
Main ()
{
int a=0113755;
printf ("%d", a>>1);
}


7, bitwise operation Assignment operator

The bitwise operator and the assignment operator can form compound assignment operators.
For example: &=, |=, >>=, <<=,∧=
Example: A & = b equals A = A & B
A << = 2 equals a = a << 2

C language as arithmetic and assignment problem of two variables

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.