Get the App
SLTechnology News&Howtos  ›  Development  › 

How to express signed and unsigned numbers in C language

Shulou Source: shulou.com Published: 2022-05-31 20:34:44 10月02日 Update

This article is a detailed introduction to "how to express signed and unsigned numbers in C language". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to express signed and unsigned numbers in C language" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

1. Symbol bits in computers

A symbol used to identify data in the most significant bits of a data type.

The highest digit is 1, indicating that the number is negative.

The highest digit is 0, indicating that the number is positive.

Here's a code to determine the sign of the data:

#include int main(){ char c = -5; short s = 6; int i = -7; printf("%d\n", ( (c & 0x80) != 0 )); printf("%d\n", ( (s & 0x8000) != 0 )); printf("%d\n", ( (i & 0x80000000) != 0 )); return 0;}

The output is as follows:

The core idea of this code is to determine whether the highest bit is 1, and then do logical operations, if it is 1, then the operation is 1, otherwise it is 0.

II. Representation of signed numbers

Complements represent signed numbers inside a computer

The complement of a positive number is the positive number itself.

The complement of a negative number is the absolute value of a negative number.

For example:

8-bit integer 5 's complement is: 0000 0101

8-bit integer-7 complement is: 11111001

16-bit integer 20 's complement is: 0000 0000 0001 0100

16-bit integer- 13 complement is: 1111 1111 1111 0011

3. Representation of unsigned numbers

Unsigned numbers are represented internally by primitive codes

unsigned number defaults to positive

unsigned number has no sign bit

For unsigned numbers of fixed length

MAX_VALUE + 1 --> MIN_VALUE

MIN_VALUE - 1 --> MAX_VALUE

Signed and unsigned

C variables default to signed types

unsigned keyword declares variables to be unsigned types

Note: Only integer types in C can declare unsigned variables.

Here's a code for unsigned numbers meeting signed numbers:

#include int main(){ unsigned int i = 5; int j = -10; if( (i + j) > 0 ) { printf("i + j > 0\n"); } else { printf("i + j

Tags: Number complement symbol language integer type output highest positive result code variable that is data article computer negative maximum content maximum Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia MySQL Shulou Technology Linux