C language bit operation

Source: Internet
Author: User
Tags bitwise bitwise operators

Bitwise operators

1, Bit and &

(1) A & bit with, two && is logical with

(2) Truth table: 1&0=0 0&1=0 0&0=0 1&1=1

2, bit or |

(1) One | bit or, two | | is logical OR

(2) Truth table: 1|0=1 0|1=1 0|0=0 1|1=1

3, position to take the reverse ~

(1) Note: C-language is the inverse of the ~,c language is the logical inversion is!

(2) The bitwise inversion is the bits of the operands by the bitwise inversion (1 becomes 0, 0 becomes 1);

And logical inversion is true (in C, as long as not 0 of any number is true) becomes false (in C language only 0 is false), False becomes true.

-------------------------------------------------------------------------------------------

int main ()

{

unsigned int a=45;

unsigned int b,c;

b = ~a;

c =!a;

printf ("B =%u\n", b);

printf ("c =%d\n", c);

}

0x2D = 10 1101

= 01 0010

-------------------------------------------------------------------------------------------

Experiment:

Any number that is not 0 is logically reversed and then 1 is obtained;

Any number of non-0-fold counter-inversion will get himself;


Bit XOR ^

(1) XOR truth table: 1^1=0 0^0=01^0=1 0^1=1

(2) Bit XOR characteristics: 2 Number if the equality result is 0, the unequal result is 1.

Memory method: XOR is the difference or operation.


A summary of the characteristics of a bit and, a bit, or a different position:

Bit with: (any number, in fact, is 1 or 0) with 1 bits with no change, with 0 bits and becomes 0

Bit or: (any number, in fact, 1 or 0) with 1 bit or become 1, with 0 bit or no change

Bit XOR: (any number, in fact, 1 or 0) with 1-bit XOR will be reversed, with 0-bit XOR or no change


Left shift << with right shift >>

The shift of C language depends on the data type.

For unsigned numbers, right-hand 0 (equivalent to logical shift) when moving left

For unsigned numbers, left 0 (equivalent to logical shift) when moving right

For signed numbers, the right side of the left shift is 0 (called arithmetic shift, equivalent to logical shift)

For signed numbers, the left-hand complement sign bit when moving right (if positive number is 0, negative is 1, called arithmetic shift)


The shift in embedded research and the displacement used are all unsigned numbers.


(1) ARM is a unified memory and IO address, ARM has a lot of internal peripherals, the SOC in the CPU through to these internal peripherals

Registers write some specific values to manipulate the internal peripherals, thus manipulating the hardware actions. So it can be said: Read and write registers is the control hardware.


(2) The register is characterized by a bitwise planning and use. But the registers are read and write, and the whole 32 bits are

(That is, you only want to modify the BIT5~BIT7 is not possible, must be whole 32bit write all)


(3) Register operation requirements are: in the setting of special positioning can not affect the other bits.


(4) How to do it? The answer is: read-change-write trilogy.


Read the rewriting of the operation concept, that is: when I want to change a certain bit of a register, I will not directly to write to him, I will read out the total value of the register, and then on this basis I want to modify the special location, and then write the modified value of the whole register.


The result is that the value of the bit I care about has been modified without affecting the original value of the other bits.

-----------------------------------------------------------------------------------------

Specific location 0 &

& 0

int main ()

{

unsigned int a = 0XAAAAAAAA;

unsigned int b = 0xffff00ff;

unsigned int c;


c = A & B;

printf ("0x%x\n", c);

}

Specific Position 1 |

| 1

Specific bit take counter ^

^ 1

------------------------------------------------------------------------------------------

For register-specific positions 0, 1, reverse

Build a special number beforehand, this number and the original value of the & | ^


Write code to construct a specific binary number with bit manipulation symbols (mainly shift and reverse)


(1) binary number with BIT3~BIT7 1 (all other bits 0)

(0x1f << 3) 5 1 shift left 3-bit


(2) Bit3~bit7 for 1 Bit23~bit25 1 others for 0

(0x1f << 3 | 0x07 <<23)


(3) bit4~bit10 is 0 and the rest is 1.

First construct the bit4~bit10 to 1 and the remainder to 0 and then the whole inverse.

~ (0x7f << 4)


If the smaller bit is 1, the majority is 0, and a lot of 1 left shifts n bits are obtained.

If the smaller bit is 0, the majority is 1, the number of bits is first built and then reversed.

If the number of consecutive 1 (continuous 0) of the portion of more than 1, a multi-segment structure, and then each other bit or.

-------------------------------------------------------------------------------------------

Bit Operation Practical Walkthrough

Formula: To set 1 use or, to clear the petty bit and, to take the opposite or, ~ and <<, >> build a specific binary number

1, given an integer number A, set a bit3, to ensure that the other bits unchanged

BIT3 1

int A;

A = a | (1<<3);

A |= (1<<3);

2, given an integer number A, set a bit3~bit7, to ensure that the other bits unchanged


int A;

A |= (0x1f<<3); Equivalent to (0B11111<<3);

3, given an integer number A, clear the bit15 of a, to ensure that the other bits unchanged

int A;

A &= ~ (1<<15);

bit15 0 The remainder is 1 (mostly 1), build the bit inverse, then take the inverse

Bit15 1 The remainder is 0 (1<<15) and then reversed ~ (1<<15)


4, given an integer number A, clear the bit15~bit23 of a,。。。


bit15~bit23:000000000 the remainder is 1 1 more than 0 less, build a bit inverse number

A &= ~ (0X1FF<<15);


5, given an integer number A, take out a bit3~bit8


1-The BIT3~BIT8 is not changed, the remaining bits are all zeroed

2-In the right shift 3bit

A &= 0x3f<<3;

A >>= 3;


6, in C language given a register of bit7~bit17 assignment 937 (the remaining bits are not affected)

A &= ~ (0x7ff<<7);

A |= (937<<7);


7, in C language given a register of the value of the bit7~bit17 plus 17


-1 First read the value of the original bit7~bit17

Add 17 to this value

-The bit7~bit17 will be zeroed out

The value of the second step is written to bit7~bit17


int A;

B = A & (0x3ff << 7);

b >>= 7;

b + = 17;

A &= ~ (0x3ff << 7);

A |= (b << 7);


8, in C language to a register of bit7~bit17 assignment value 937, at the same time to Bit21~bit25 assignment 17.


1.

int A;

A &= ~ (0x3ff << 7);

A |= (937 << 7);


A &= ~ (0x1f << 21);

A |= (<< 21);

2.

int A;

A &= ~ ((0x3ff<<7) | (0x1f<<21));

A |= ((937<<7) | (17<<21));


-------------------------------------------------------------------------------------------

Macro definition to complete bitwise operations


1. Use the macro definition to set the nth bit of the 32-digit x (the right, that is, the first bit of the bit0) position


#define SET_BIT_N (X,n) (x | (1u<< (n-1)))


2. Use the macro definition to clear the nth bit of the 32-digit X


#define CLEAR_BIT_N (X,n) (X & ~ (1u<< (n-1)))


3. Use a macro to define the nth bit of the 32-digit x to the position of the first m


Analysis n=3 m = to Bit2 to bit5 position

Need a formula to (M-N+1) a 1


32 bits 1 ~0u first

---the number of the first step to the right X-position to get (m-n+1) a 1

((~0u) >> (32-(m-n+1)) << (n-1)


#define SET_BIT_N_M (x,n,m) (x| ...)


4. Intercept the nth to the M bits of the variable x


#define GETBITS (X,n,m) ((X & ~ (~ (0U) << (m-n+1)) << (n-1)) >> (n-1))


Several points

(X & ~ (~ (0U) << (m-n+1)) << (n-1)) >> (n-1)

Analyze why >> (n-1)

-

X & ~ (~ (0U) << (m-n+1)) << (n-1)

-

~ (~ (0U) << (m-n+1)) << n-1

-------------------------------------------------------------------------------------------

Write code test operator precedence level ~ <<

A = 0xf;

b = ~a<<4;

FFFFFF00 First ~ after <<

ffffff0f First << after ~

-------------------------------------------------------------------------------------------


This article from the "8342724" blog, reproduced please contact the author!

C language bit operation

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.