想请问一下这个为什么会是这样,我多输入一位就变成Usigned Long Long 了,运行的话就提示我要收缩转换
**
这个问题怎么解决啊,我不想在少一位的情况下解决这个收缩转换的问题,或者说怎么进行收缩转换以后就可以进行异或运算
**
用=给数字类型变量赋值的时候,编译器会根据=右侧的数字大小自动匹配最合适的数据类型。在你的代码里,虽然用LL来说明值是long long类型,但是,如果这个值超出了long long类型的范围,那么,编译器就会自动给他提升到合适的类型,你的这个代码中,就会自动提升为 unsigned long long类型。
修改方法:
将三个变量类型都改为unsigned long long,如下:
unsigned long long m_code{ 930529060092641281 };//硬件码
unsigned long long test_code{ 0xF2349876EF56CA25LL };//测试码long long mx = test code m code
unsigned long long mx = test_code^ m_code;
这跟c++设定的规则有关系,
常量也有类型
C Integer Constants
An “integer constant” is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed.
Syntax
integer-constant :
decimal-constant integer-suffix opt
octal-constant integer-suffix opt
hexadecimal-constant integer-suffix opt
decimal-constant :
nonzero-digit
decimal-constant digit
octal-constant :
0
octal-constant octal-digit
hexadecimal-constant :
0x hexadecimal-digit
0X hexadecimal-digit
hexadecimal-constant hexadecimal-digit
nonzero-digit : one of
1 2 3 4 5 6 7 8 9
octal-digit : one of
0 1 2 3 4 5 6 7
hexadecimal-digit : one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix :
unsigned-suffix long-suffix opt
long-suffix unsigned-suffix opt
unsigned-suffix : one of
u U
long-suffix : one of
l L
64-bit integer-suffix :
i64
Integer constants are positive unless they are preceded by a minus sign (–). The minus sign is interpreted as the unary arithmetic negation operator. (See Unary Arithmetic Operators in Chapter 4 for information about this operator.)
If an integer constant begins with the letters 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal.
The following lines are equivalent:
0x1C /* = Hexadecimal representation for decimal 28 /
034 / = Octal representation for decimal 28 */
No white-space characters can separate the digits of an integer constant. These examples show valid decimal, octal, and hexadecimal constants.
/* Decimal Constants */
10
132
32179
/* Octal Constants */
012
0204
076663
/* Hexadecimal Constants */
0xa or 0xA
0x84
0x7dB3 or 0X7DB3
这个是有符号和无符号之间的转换问题,和Long Long无关
你给的值已经超过long long可存储范围, 没有负号, 却存了超过非负数能容纳的范围, 逻辑上应该是ull, 但赋值强制转为ll, 会变负值.
对于你这个有按位操作的计算, 最好都变成ull .
你就改成 unsigned long long就行了
#include <iostream>
int main(){
unsigned long long m_code{930529060092641281};
unsigned long long test_code{0xF2349876EF56CA25};
unsigned long long mx = test_code ^ m_code ;
std::cout << std::hex << mx;
}
long long
unsigned long long
它们至少64位
long long int lli1 = -9000000006854775808LL; unsigned long long int lli2 = 9000000006854775808LL; long long ll_min = LLONG_MIN; long long ll_max = LLONG_MAX; unsigned long long ull_max = ULLONG_MAX; LOG("min of long long: %lld",ll_min); LOG("max of long long: %lld",ll_max); LOG("max of unsigned long long: %llu",ull_max);
查看long long 大小 引入头文件<climits>(或<limits.h>) 最好使用 <climits>
根据参考资料中的内容,我们可以得知Long Long类型的字节大小为8,表示范围为-9223372036854775808 ~ 9223372036854775807。当你输入一个额外的位数时,超出了Long Long类型能表示的范围,导致自动转换为Unsigned Long Long类型。这个转换会提示收缩转换,意思是你的数据超出了原类型的表示范围,丢失了一些信息。解决这个问题,你可以考虑使用unsigned long long类型,其字节大小也为8,但表示范围为0 ~ 18446744073709551615,这样就可以保留你输入的所有位数了。以下是代码示例:
#include <iostream>
using namespace std;
int main() {
unsigned long long num;
cin >> num;
cout << "The number is: " << num << endl;
return 0;
}
这段代码中使用了unsigned long long类型来接收输入的数字。如果你可以提供更多的具体信息,比如你具体输入了什么数,代码有什么错误提示,那我们可以对问题提供更详细的解决方案。
在C++中, unsigned long long
是一种无符号整数类型,它表示的整数范围比 unsigned int
、unsigned long
、long long
和 int
类型都要大。
要使用 unsigned long long
类型,您需要在变量声明之前添加 unsigned
和 long long
关键字。例如:
unsigned long long myNum = 1234567890;
请注意,在使用 unsigned long long
类型时,也可以使用 ULL
后缀,它表示一个无符号长长整数。例如:
unsigned long long myNum = 1234567890ULL;
此外,需要注意使用 unsigned long long
类型时要留意数据类型的上限,以避免数据类型溢出的问题。在大数据量处理时,建议使用高精度计算库来对数据进行处理。