Common Techniques for Register Manipulation in C Language

Common Techniques for Register Manipulation in C Language

When assigning values to registers using the C language, bit manipulation methods of the C language are often required.

Clearing a Specific Bit of a Register

Assume a represents the register, which already has a value. If we want to clear a specific bit while keeping other bits unchanged, the code is as follows.

// Define a variable a = 1001 1111 b (binary number)unsigned char a = 0x9f;// Clear bit2a &= ~(1<<2);// The 1 in parentheses is left shifted by two bits, (1<<2) gives binary number: 0000 0100 b// Bitwise NOT, ~(1<<2) gives 1111 1011 b// If the original value of a is in binary: a = 1001 1111 b// The result number and a perform a bitwise AND operation, a = (1001 1111 b)&(1111 1011 b),// After the operation, the value of a becomes a=1001 1011 b// The bit2 of a is cleared, while other bits remain unchanged.

Clearing Several Consecutive Bits of a Register

Since there may be several consecutive register bits used to control a certain function, assume we need to clear several consecutive bits of the register while keeping other bits unchanged, the code is as follows.

// If we divide the binary bits of a into groups of 2// that is, bit0, bit1 is the 0th group, bit2, bit3 is the 1st group,// bit4, bit5 is the 2nd group, bit6, bit7 is the 3rd group// To clear bit2 and bit3 of the 1st groupa &= ~(3<<2*1);// The 3 in parentheses is left shifted by two bits, (3<<2*1) gives binary number: 0000 1100 b// Bitwise NOT, ~(3<<2*1) gives 1111 0011 b// If the original value of a is in binary: a = 1001 1111 b// The result number and a perform a bitwise AND operation, a = (1001 1111 b)&(1111 0011 b),// After the operation, the value of a becomes a=1001 0011 b// The bit2 and bit3 of the 1st group of a are cleared, while other bits remain unchanged.// In the above (~(3<<2*1)), (1) is the group number; to clear the 3rd group bit6, bit7 should be 3 here// (2) is the number of bits per group, there are 2 binary bits per group; if divided into 4 bits per group, it should be 4 here// (3) is the value when all bits in the group are 1; if divided into 4 bits per group, this should be the binary number "1111 b"// For example, to clear bit4 and bit5 of the 2nd groupa &= ~(3<<2*2);

Assigning Values to Specific Bits of a Register

After clearing the bits of the register, it is convenient to write the necessary values to specific bits. The specific code is as follows.

// a = 1000 0011 b// Now set the cleared bit4 and bit5 of the 2nd group to the binary number "01 b" a |= (1<<2*2);// a = 1001 0011 b, successfully set the value of the 2nd group, while other groups remain unchanged

Inverting a Specific Bit of a Register

To perform an inversion operation on a specific bit of the register, i.e., changing 1 to 0 and 0 to 1, this can be done directly using the following operation.

// a = 1001 0011 b// Invert bit6, while keeping other bits unchanged a ^=(1<<6);// a = 1101 0011 b

Reference Format: Feng Dan, Luo Ling, Li Yu, et al. Exploration of Program Design Courses Integrated with Ideological and Political Education[J]. Computer Education, 2022(9): 72—76.

(Editing by WeChat: Shi Zhiwei)

(End)

More Exciting:

Yan Shi │ Thoughts and Suggestions on the “Predicament” of Young Teachers in Colleges

【Directory】 Computer Education 2022 Issue 8

【Directory】 Computer Education 2022 Issue 7

【Directory】 Computer Education 2022 Issue 6

【Directory】 Computer Education 2022 Issue 5

【Editorial Board Message】 Professor Li Xiaoming of Peking University: Thoughts from the “Year of Classroom Teaching Improvement”…

Professor Chen Daoxu of Nanjing University: Teaching students to ask questions and teaching students to answer questions, which is more important?

【Yan Shi Series】: Development Trends in Computer Science and Their Impact on Computer Education

Professor Li Xiaoming of Peking University: From Interesting Mathematics to Interesting Algorithms to Interesting Programming—A Path for Non-Majors to Experience Computational Thinking?

Several Issues to Consider in Building a First-Class Computer Discipline

New Engineering and Big Data Major Construction

Other Stones Can Serve to Polish Jade—Compilation of Research Articles on Computer Education at Home and Abroad

Common Techniques for Register Manipulation in C Language

Common Techniques for Register Manipulation in C Language

Leave a Comment