C Language -- operator priority

Source: Internet
Author: User

For uncertainty issues, find a way to confirm, or wait for you will be a Bug-programming experience

When I added a control and monitoring module to the project in Nanjing last month, I wrote the Code. The Code contains the following sentence:
[Cpp]
If (indicator & 0x8000 = 0x8000 ){
Do something
}
Here, indicator is int type. I mean, if the 16th bits of indicator are 1, execute the statement in brackets, that is, the 16th bits of indicator are a flag. I think so much when writing code, and I think so many days after writing it. However, my buddy in Nanjing told me that the program is not running correctly. If I look at the code myself, I still find that there is no problem. However, I have been told many times that computers will not treat a programmer casually. Since the program is incorrect, the Bug actually exists. After investigation by all parties, I concentrated my focus on the following sentence:
[Cpp]
Task. cur = indicator & 0x7fff;
The intention of this sentence is to remove the flag in indicator and restore the correct value. As mentioned above, indicator is of the int type, that is, 32, while 0x7fff is of the 16 type. So I naturally thought the calculation may be wrong. As you may see, you may have to smile, "you have a tea experience !". Haha, I also Laughed afterwards. No way. When the culprit is not found, the scapegoat is a good way to reduce the psychological pressure. In fact, the calculation here does not cause errors because C has a type improvement. However, I still want to know how the compiler processes constants such as 0x7fff.
Well, then we will do justice for the scapegoat, so we need to find out the culprit. The truth is that "the priority of Relational operators is higher than that of logical operators ". For the operator priority issue, every time I read books on C Programming, I will always jump over, which is too troublesome. I hate to remember those troublesome things, so it becomes an issue of programming uncertainty. However, it is always good to know. However, I still don't remember it. I only leave a consciousness for myself. I will solve such problems in the future. Therefore, even if I do not know the priority, I will write the following statement:
[Cpp]
If (indicator & 0x8000) = 0x8000 ){
Do something
}

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.