长型64位的Linux [英] Long type 64bit linux

查看:254
本文介绍了长型64位的Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题球员,但也许我只是忘了什么东西。
在64位Linux中,一个长8个字节是否正确?
如果是这样的话,我想设置的第64位,我可以做到以下几点:

 无符号长NUM = 1<< 63;

每当我编译这一点,但是,它给了我一个错误,说我左移比宽度多。
另外,如果我想需要很长的类型的第32位(无符号扩展),可我做的:

  NUM = NUM​​&放大器; 0xFFFFFFFF的;

或者怎么样:

  NUM =(INT)(NUM);

感谢您。


解决方案

  

在64位版,而长8个字节是否正确?


需要不是。依赖于编译器不是底层操作系统。勾选此一个不错的讨论。
什么决定的sizeof整数?


  

每当我编译这一点,但是,它给了我一个错误,说我
  左移位由以上的宽度


大家都已经回答了这一点。使用 1UL


  

另外,如果我想采取一个类型的第32位(不
  符号扩展),我可以这样做:


  NUM = NUM​​&放大器; 0xFFFFFFFF的;
或者怎么样:NUM =(INT)(NUM);

NUM = NUM​​&放大器; 0xFFFFFFFF的。这会给你低32位。但要注意,如果您的系统上只有4个字节,那么你得到的整个号码的。即将符号扩展的一部分,如果你已经使用了,而不是无符号长那么你不能做掉符号扩展位。例如, 1 重新psented所有那些$ P $,直接从第0位。您将如何通过屏蔽避免这些的?

NUM =(INT)(NUM)会给你低32位,但编译器可能会通过溢出异常警告,如果 NUM 不适合的 INT

Very simple questions guys, but maybe I'm just forgetting something. In 64bit linux, a long is 8bytes correct? If that's the case, and I want to set the 64th bit, I can do the following:

unsigned long num = 1<<63;

Whenever I compile this, however, it gives me an error saying that I'm left shifting by more than the width. Also, if I wanted to take the first 32bits of a long type (without sign extension), can I do:

num = num&0xFFFFFFFF;

or what about:

num = (int)(num);

Thank you.

解决方案

In 64bit linux, a long is 8bytes correct?

Need not be. Depends on the compiler than on the underlying OS. Check this for a nice discussion. What decides the sizeof an integer?

Whenever I compile this, however, it gives me an error saying that I'm left shifting by more than the width

Everyone have already answered this. Use 1UL

Also, if I wanted to take the first 32bits of a long type (without sign extension), can I do:

num = num&0xFFFFFFFF;
or what about:

num = (int)(num);

num = num&0xFFFFFFFF. This will give you the lower 32-bits. But note that if long is just 4 bytes on your system then you are getting the entire number. Coming to the sign extension part, if you've used a long and not unsigned long then you cannot do away with the sign extended bits. For example, -1 is represented as all ones, right from the 0th bit. How will you avoid these ones by masking?

num = (int)(num) will give you the lower 32-bits but compiler might through a Overflow Exception warning if num does not fit into an int

这篇关于长型64位的Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆