---
title: "C语言int&unsigned int与二进制类型"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/c-int-unsigned-int-binary-types/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/02-编程语言与运行时/C++/C语言int&unsigned int与二进制类型.md"
content_hash: ef996b115c4665ca1690df54d3878f3d6e0785b57d7d3ffb905957894ab68c50
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# C语言int&unsigned int与二进制类型

﻿# C语言int&unsigned int与二进制类型

> 创建时间：2020/3/31 11:52

## C语言int&unsigned int与二进制类型

**任何数据都是以其二进制的补码形式存储在内存中的**
int占几个字节是与电脑编译器有关的。不过现在大部分电脑int占4个字节，即32位

### unsigned int

32个0~32个1，即：0~4294967295

short int 是16位的，无符号的范围是0~65535

### ((signed)int)

int实际上是signed int ，我们通常省略signed
补码表示
范围：-2147483648~2147483647

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

### char和int的转换

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

int转换成char会切割
char转换成int又会在前面全加上1

130转换成char的时候它在内存中变成了
1000 0010
再转换成int类型就会变成
1111 1111 1111 1111 1111 1111 1000 0010（
补码）

输出的时候要输出原码
转换成原码：
1000 0000 0000 0000 0000 0000 0111 1110
所以最后的结果就是-126
