简体   繁体   English

在x86_64芯片(64位服务器)上运行的C ++ unix 32位到Red Hat Linux

[英]C++ unix 32bit to Red Hat Linux running on an x86_64 chip (64-bit server)

I am working on a project that is porting application in C++ from 32 bit unix to Red Hat Linux running on an x86_64 chip (64-bit server). 我正在一个项目中,该项目将C ++中的应用程序从32位Unix移植到在x86_64芯片(64位服务器)上运行的Red Hat Linux。 I am facing issues related to long data types.I wrote a sample program to print a binary long and unsigned long value of 1. This has an extra 1 in middle: 我遇到了与长数据类型有关的问题。我编写了一个示例程序来打印二进制long和无符号long值1。中间有一个额外的1:

ul = 0000000000000000000000000000000100000000000000000000000000000001 ul = 0000000000000000000000000000000000000100000000000000000000000000000001
l = 0000000000000000000000000000000100000000000000000000000000000001 l = 0000000000000000000000000000000000000100000000000000000000000000000000000001

string binary(unsigned long i)   
{   
int n = sizeof(unsigned long);  
string s;  
n *= 8;  
--n;  
while (n >= 0)   
{  
unsigned long mask = (1 << n);   
s += (mask & i ? "1" : "0");   
--n;  
}   
return s;  
}   
string binary(long i)  
{  
int n = sizeof( long);   
string s;   
n *= 8;  
--n;  
while (n >= 0)  
{   
long mask = (1 << n);   
s += (mask & i ? "1" : "0");  
--n;   
}   
return s;  
}   
int main()  
{  
unsigned long ul = 1;  
long l = 1;    
cout << "sizeof ul = " << sizeof ul <<  ", ul = "  <<ul<<  endl;   
cout << "sizeof l = "  << sizeof l  << ", l = " << l << endl;    
cout << "ul = " << binary(ul) << endl;   
cout << "l = "  << binary(l) << endl;    
return 0;  
}      

As you all can see I am not getting why there is an extra 1 IN MIDDLE. 大家都知道,我不明白为什么要多加1英寸中间价。 This is causing run time issues in my application. 这在我的应用程序中导致运行时问题。 Please let me know. 请告诉我。 I am getting extra 1 middle in Red Hat Linux running on an x86_64 chip (64-bit server) not in unix and that is the cause of run time issues. 我在x86_64芯片(64位服务器)上而不是在unix上运行的Red Hat Linux中得到了额外的1分,这是运行时问题的原因。 Any idea please let me know. 有什么想法请告诉我。

The problem is here: 问题在这里:

unsigned long mask = (1 << n);  

Apparently, a unsigned long has 8 byte and a normal int has 4 byte on your system. 显然,在您的系统上, unsigned long有8个字节,而普通int有4个字节。

Here you have a int with value 1 (every literal value is int if nothing else is explicitely specified). 这里有一个int值为1(每字面值为int如果没有其他明确地规定)。 You´re doing the shifting on this 4 byte int , and then the result is converted to 8 byte to save it in mask . 您正在对此4字节int进行移位, 然后将结果转换为8字节以将其保存在mask Shifting more than 31 bit is too much in 4 byte, yet you´re doing it until 63 bit. 在4字节中移出超过31位是太多了,但是直到63位为止。

To solve the problem, you have to tell the compiler that 1 is meant a 8 byte unsigned long . 要解决该问题,您必须告诉编译器1表示8字节unsigned long
Three possibilites: 三种可能性:

unsigned long mask = (1UL << n);  
//or
unsigned long mask = ((unsigned long)1 << n);  
//or just
unsigned long mask = 1;  
mask = mask << n;  

The other function (for the signed type) has the same problem. 另一个函数(用于带符号的类型)具有相同的问题。
With the first variant, use just L there, without U . 对于第一个变体,在那里只使用L ,而没有U

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM