Knowing an integer, how to judge that this integer is unsigned?

foreword

Record a question shared by Brother Ken in the group on June 11. To be honest, I didn't understand after thinking about the topic for a long time. After looking at the answer, I realized that this question is really simple.

topic

Knowing an integer, how to judge that this integer is unsigned?

answer

(1) Because my thinking is completely wrong, I will no longer conduct random analysis here. The hint given by Kenge is: the methods of AND, OR, NOT, XOR in C language are good things. Those who are interested can think about it here.

insert image description here

(2) The answer is revealed, it is a > 0 and ~a > 0 that a big guy in the group said. I have to admit the gap, and it was immediately reflected, and I still have a lot to learn. The answer given by Brother Ken is
#define ISUNSIGN(A) ((A)>=0 && ~(A)>=0)
(3) After this is used to invert, the sign bit will also be inverted. To be honest, I confuse this knowledge point, thinking that the sign bit will not be reversed.
(4) First of all, because if a number is negative, then it must be signed data.
(5) Secondly, '~' performs bit inversion. At the same time the sign bit will also change. For unsigned data, it has no effect, because unsigned data does not have negative numbers. And if it is signed data, then the positive number will become negative. Therefore, judgment cannot be passed.

Summarize

(1) There are too many knowledge points of C language, and it is easy to forget. I flipped through my blog, and there is actually a special explanation of logical inversion and arithmetic inversion. (Crying and laughing)
(2) Knowledge needs to be consolidated repeatedly, and I learned something today. happy

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/131158874