相关疑难解决方法(0)

什么是unsigned char?

在C/C++中,unsigned char用于什么?它与常规有什么不同char

c c++ char

456
推荐指数
13
解决办法
55万
查看次数

char默认是签名还是未签名?

在"C的完整参考"一书中,提到char默认情况下是无符号的.

但我试图用GCC和Visual Studio验证这一点.它默认签名.

哪一个是正确的?

c signed types char

145
推荐指数
5
解决办法
8万
查看次数

"int"和"uint"/"long"和"ulong"有什么区别?

我知道intlong(32位和64位数字),但是什么uintulong

c# unsigned signed types integer

99
推荐指数
4
解决办法
15万
查看次数

char!=(signed char),char!=(unsigned char)

下面的代码编译,但char类型的行为与int类型的行为不同.

特别是

   cout << getIsTrue< isX<int8>::ikIsX  >() << endl;
   cout << getIsTrue< isX<uint8>::ikIsX  >() << endl;
   cout << getIsTrue< isX<char>::ikIsX  >() << endl;
Run Code Online (Sandbox Code Playgroud)

导致三种类型的模板的3个实例化:int8,uint8和char.是什么赋予了?

对于ints来说也是如此:int和uint32导致相同的模板实例化,而signed int则是另一个.

原因似乎是C++将char,signed char和unsigned char视为三种不同的类型.而int与signed int相同.这是对的还是我错过了什么?

#include <iostream>

using namespace std;

typedef   signed char       int8;
typedef unsigned char      uint8;
typedef   signed short      int16;
typedef unsigned short     uint16;
typedef   signed int        int32;
typedef unsigned int       uint32;
typedef   signed long long  int64;
typedef unsigned long long uint64;

struct TrueType {};
struct FalseType {};

template <typename T>
struct isX …
Run Code Online (Sandbox Code Playgroud)

c++ char

40
推荐指数
4
解决办法
2万
查看次数

c ++中char和signed char之间的区别?

请考虑以下代码:

#include <iostream>
#include <type_traits>

int main(int argc, char* argv[])
{
    std::cout<<"std::is_same<int, int>::value = "<<std::is_same<int, int>::value<<std::endl;
    std::cout<<"std::is_same<int, signed int>::value = "<<std::is_same<int, signed int>::value<<std::endl;
    std::cout<<"std::is_same<int, unsigned int>::value = "<<std::is_same<int, unsigned int>::value<<std::endl;
    std::cout<<"std::is_same<signed int, int>::value = "<<std::is_same<signed int, int>::value<<std::endl;
    std::cout<<"std::is_same<signed int, signed int>::value = "<<std::is_same<signed int, signed int>::value<<std::endl;
    std::cout<<"std::is_same<signed int, unsigned int>::value = "<<std::is_same<signed int, unsigned int>::value<<std::endl;
    std::cout<<"std::is_same<unsigned int, int>::value = "<<std::is_same<unsigned int, int>::value<<std::endl;
    std::cout<<"std::is_same<unsigned int, signed int>::value = "<<std::is_same<unsigned int, signed int>::value<<std::endl;
    std::cout<<"std::is_same<unsigned int, unsigned int>::value = "<<std::is_same<unsigned int, unsigned int>::value<<std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ signed char type-traits c++11

26
推荐指数
3
解决办法
1万
查看次数

签署一个字符是什么意思?

鉴于有符号和无符号整数使用相同的寄存器等,并且只是不同地解释位模式,而C字符基本上只是8位整数,C中有符号和无符号字符之间的区别是什么?我理解char的签名是实现定义的,我根本无法理解它是如何产生影响的,至少当char用于保存字符串而不是数学时.

c string signed char character-encoding

19
推荐指数
3
解决办法
3万
查看次数

使用char或unsigned char数组存储原始数据更好吗?

当需要在内存中缓冲一些原始数据时,例如从流中,是否更好地使用char或unsigned char数组?我总是使用char,但在工作中说它是更好的unsigned char,我不知道为什么......

c c++

7
推荐指数
3
解决办法
2577
查看次数

C++中有符号和无符号之间的转换

考虑以下C++代码:

#include <cstdio>

using namespace std;

int main()
{
    int ia = -5;
    unsigned int uia = ia;
    char ca = -5;
    unsigned char uca = ca;

    printf("%d\n", (ia == uia));
    printf("%d\n", (ca == uca));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

1
0
Run Code Online (Sandbox Code Playgroud)

我不明白有什么之间的区别int,并char同时从铸造signedunsigned

你能开导我吗?

c++ casting

5
推荐指数
1
解决办法
2260
查看次数

标准是否保证uint8_t,int8_t和char都是唯一类型?

似乎可以保证通过以下要求(已经在这里提出了要求):

#include <type_traits>
static_assert(!std::is_same_v<char, signed char>);
static_assert(!std::is_same_v<char, unsigned char>);
Run Code Online (Sandbox Code Playgroud)

引用cppreference

[ char]与signed char或具有相同的表示和对齐方式unsigned char,但始终是不同的类型

难道这也保证了int8_tuint8_t在明确有符号类型来定义 在来定义char的,因此也形成了一套3种不同类型的用char

#include <cstdint>
#include <type_traits>
static_assert(!std::is_same_v<char, int8_t>);
static_assert(!std::is_same_v<char, uint8_t>);
Run Code Online (Sandbox Code Playgroud)

c++ char language-lawyer cstdint

5
推荐指数
2
解决办法
92
查看次数

字节类型:std::byte vs std::uint8_t vs unsigned char vs char vs std::bitset&lt;8&gt;

C++ 有很多类型模糊地描述相同的事物。假设我们正在针对一个字节为 8 位的架构进行编译,则以下所有类型都大致相似:

  • std::byte
  • std::uint8_t
  • std::bitset<8>
  • unsigned char(8 位)
  • char(8 位)

如果一个字节是 8 位,那么所有这些类型或多或少可以互换吗?如果没有,什么时候需要使用一个而不是另一个?

我经常在 Stack Overflow 上看到诸如将十六进制字符串转换为字节数组之类的问题,其中有人使用std::uint8_tcharunsigned char其他类型来表示“字节”。这只是风格偏好的问题吗?


注意:此问答旨在成为社区常见问题解答,鼓励进行编辑。std::byte尽管 C++17 已经引入了这似乎使得选择变得显而易见,但何时使用“字节”的类型以及为什么的问题始终出现。std::bitset提供一个常见问题解答来解决有关、std::uint8_t等作为“字节”的所有误解是很有用的。鼓励编辑。

c++ byte c++17 std-byte

4
推荐指数
1
解决办法
933
查看次数

C中无符号变量的算术

如何无符号的字符,例如,从取值-128+127?根据我的理解,最重要的位用于表示数字的符号,而字符的剩余位用于表示数字的大小.现在,7位的最大可能幅度是127,所以范围不应该从?-127+127?怎样才能-128有一个结果?

其次,以下行为背后的位级逻辑是什么

#include <stdio.h>

int main()
{
    signed char x = 127;
    x += 1;
    printf("%i", x);
}
Run Code Online (Sandbox Code Playgroud)

输出:

-128
Run Code Online (Sandbox Code Playgroud)

人们可以看到,x成为-128,但为什么呢?这种行为背后的算法是什么?

c unsigned undefined-behavior twos-complement

2
推荐指数
1
解决办法
335
查看次数