4605 views|16 replies

6818

Posts

11

Resources
The OP
 

C language sets/gets the value of a certain bit in a variable [Copy link]

 
7分芯积分 * 回复本帖可获得 1 分芯积分奖励! 每人限 1 次
 

In operating registers, you need to set/get the value of a certain bit.

#define GetBit(m,n) ((m)>>y)&0x01) //Get the value of the nth bit of m and return 0 or 1

#define SetBit(m,n) m |= (1<<n) //Set the n bit of m to 1

#define ClrBit(m,n). m &= ~(1<<n) //Clear bit n of m

Happy New Year! Reply to this post and you will get a reward!

This post is from Domestic Chip Exchange

Latest reply

Simple, direct, clear, and efficient. This is how 51 is basically operated. It has many logical operations and high operating efficiency.   Details Published on 2023-2-17 13:52
 
 

820

Posts

2

Resources
2
 

回帖奖励 +1 分芯积分

Haven't come across any collections yet.

This post is from Domestic Chip Exchange
 
 
 

2624

Posts

6

Resources
3
 

回帖奖励 +1 分芯积分

Reminded me of the bit operation of the msp430 microcontroller


This post is from Domestic Chip Exchange
 
 
 

112

Posts

0

Resources
4
 

回帖奖励 +1 分芯积分

Learned

This post is from Domestic Chip Exchange
 
 
 

48

Posts

0

Resources
5
 

回帖奖励 +1 分芯积分

Learned, collected

This post is from Domestic Chip Exchange
Personal signature

~~高性价比APT32单片机/MCU~~

 
 
 

4764

Posts

12

Resources
6
 

回帖奖励 +1 分芯积分

Directly operate on the address according to the memory bit operation

This post is from Domestic Chip Exchange

Comments

Happy New Year, moderator. Have you been to your father-in-law's house? How's the effect? Are you ready to make a bunny?  Details Published on 2023-1-24 08:40
 
 
 

6818

Posts

11

Resources
7
 
Azuma Simeng published on 2023-1-23 21:21 Directly operate on the address according to the memory bit operation

Happy New Year, moderator. Have you been to your father-in-law's house? How's the effect? Are you ready to make a bunny?

This post is from Domestic Chip Exchange
 
 
 

1305

Posts

0

Resources
8
 

回帖奖励 +1 分芯积分

The basic operations of embedded systems in the 51 era were mainly due to the small amount of memory at that time. Now that MCUs have large memory, some operations are no longer done this way.

This post is from Domestic Chip Exchange

Comments

Is it faster to use bit operations? Some hal libraries now require many judgments, which means the programming speed is high but the efficiency is not high.  Details Published on 2023-1-24 12:49
 
 
 

6818

Posts

11

Resources
9
 
yang_alex posted on 2023-1-24 08:50 The basic operations of embedded systems in the 51 era were mainly due to the small amount of memory at that time. Now that MCUs have large memory, some of them are no longer done this way.

Is it faster to use bit operations? Some hal libraries now require many judgments, which means the programming speed is high but the efficiency is not high.

This post is from Domestic Chip Exchange

Comments

The low efficiency of the hal library is a thing of the past. After modern compilers enable o1 local optimization, these expressions inside the hal library will be optimized out, and the efficiency is basically the same as writing directly to registers. On the contrary, the portability and readability of the hal library are much better than those of the standard library and registers. In addition, if the timing requirements for io operations are relatively high  Details Published on 2023-1-25 09:31
The low efficiency of the hal library is a thing of the past. After modern compilers enable o1 local optimization, these expressions inside the hal library will be optimized out, and the efficiency is basically the same as writing directly to registers. On the contrary, the portability and readability of the hal library are much better than those of the standard library and registers. In addition, if the timing requirements for io operations are relatively high  Details Published on 2023-1-24 15:12
 
 
 

13

Posts

0

Resources
10
 

回帖奖励 +1 分芯积分

lugl4313820 posted on 2023-1-24 12:49 Is it faster to use bit operations? Some hal libraries now have to judge many times, and the programming speed is high, but the efficiency is not high.

The low efficiency of the hal library is a thing of the past. After modern compilers enable o1 local optimization, these expressions inside the hal library will be optimized out, and the efficiency is basically the same as writing directly to registers. On the contrary, the portability and readability of the hal library are much better than the standard library and registers. In addition, if the timing requirements for io operations are relatively high, microcontrollers that support bit-band operations can use bit-band operations, which is much faster than reading registers, doing bit operations, and then writing.


This post is from Domestic Chip Exchange
 
 
 

1305

Posts

0

Resources
11
 
lugl4313820 posted on 2023-1-24 12:49 Is it faster to use bit operations? Some hal libraries now have to judge many times, and the programming speed is high, but the efficiency is not high.

Yes. Bit manipulation is faster and is very convenient for making flags (formerly called flag bits) for multi-conditional compound judgments. The main frequency of MCUs is generally high now, and most of our needs can be met. Bit manipulation is only performed when the performance of the MCU needs to be squeezed, and in extreme cases, assembly language may be used for bit manipulation. The disadvantage of bit manipulation is that the code is more cumbersome to read (in fact, C language is not bad, you can name the variables yourself and it reads the same), and you need to be familiar with bit shift operations.

This post is from Domestic Chip Exchange

Comments

Studying bit operations can change your programming ideas. Some things that can be done in one sentence may require a lot of detours if you use a library. I wonder if you don't need to convert them after compiling.  Details Published on 2023-1-25 15:01
 
 
 

127

Posts

0

Resources
12
 

回帖奖励 +1 分芯积分

Learned

This post is from Domestic Chip Exchange
 
 
 

6818

Posts

11

Resources
13
 
yang_alex posted on 2023-1-25 09:31 Yes. Bit operations are faster and are very convenient for making flags (originally called flag bits) for multi-conditional compound judgments. Now the main frequency of MCU is generally...

Studying bit operations can change your programming ideas. Some things that can be done in one sentence may require a lot of detours if you use a library. I wonder if you don't need to convert them after compiling.

This post is from Domestic Chip Exchange
 
 
 

331

Posts

0

Resources
14
 

回帖奖励 +1 分芯积分

I am more accustomed to bit operations, which are simple, direct and transparent. . .

This post is from Domestic Chip Exchange

Comments

I didn’t understand it before, but later on, I finally figured it out and it felt like it was going on pretty quickly.  Details Published on 2023-1-31 15:27
 
 
 

6818

Posts

11

Resources
15
 
pcf2000 posted on 2023-1-31 10:51 I am more accustomed to bit operations, which are simple, direct and transparent. . .

I didn’t understand it before, but later on, I finally figured it out and it felt like it was going on pretty quickly.

This post is from Domestic Chip Exchange
 
 
 

307

Posts

4

Resources
16
 

回帖奖励 +1 分芯积分

When I was learning 32 register programming, I started to get used to operating bit by bit.

This post is from Domestic Chip Exchange
 
 
 

28

Posts

0

Resources
17
 

回帖奖励 +1 分芯积分

Simple, direct, clear, and efficient. This is how 51 is basically operated. It has many logical operations and high operating efficiency.

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list