用所有F初始化一个int变量 [英] initializing an int variable with all F's

查看:62
本文介绍了用所有F初始化一个int变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,


我只是在看一些代码在哪里初始化一个整数

使用所有F'的以下语句;


unsigned int i = -1;


我想知道这是否是正确的做事方式?如果没有,

这样做的正确方法是什么?

Guys,

I was just looking at some code where to initialize an integer
with all F''s following statement is used;

unsigned int i = -1;

I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?

推荐答案

ju ********** @ yahoo.co.in 写道:

伙计们,


我只是看一些代码在哪里用所有F'初始化一个整数

使用以下声明;


unsigned int i = -1;


我想知道这是否是正确的做事方式?如果没有,

这样做的正确方法是什么?
Guys,

I was just looking at some code where to initialize an integer
with all F''s following statement is used;

unsigned int i = -1;

I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?



unsigned int i = ~0U;

初始化为全1'。没有办法在假设的41位机器上初始化为所有

(可能是十六进制)F'。

unsigned int i = ~0U;
initializes to all 1''s. There is no way to initialize to all
(presumably, hex) F''s on a hypothetical 41-bit machine.




< ju ********** @ yahoo.co.inwrote in message

news:11 ************ **********@q4g2000prc.googlegro ups.com ...

<ju**********@yahoo.co.inwrote in message
news:11**********************@q4g2000prc.googlegro ups.com...

伙计们,


我只是看一些代码在哪里初始化一个整数

,使用所有F'的以下语句;


unsigned int i = -1;


我想知道这是否是正确的做事方式?如果没有,

这样做的正确方法是什么?
Guys,

I was just looking at some code where to initialize an integer
with all F''s following statement is used;

unsigned int i = -1;

I want to know if this is the right way of doing the things ? if not,
what is the correct way to do this ?



这是其中一个尴尬的事情。

在实践中,你永远不可能编写一个非二进制的补充机器。

但是允许一个补码和符号数量(一个地方你将

看到非二的补码整数是IEEE浮动

点数的指数。

所以i = ~0;实际上是实现目标的最佳方式。但即使在生产代码中,你也会看到我的价格是b $ b = -1。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/ ~bgy1mm


" ju ********** @ yahoo.co.in"写道:
"ju**********@yahoo.co.in" wrote:

>

我只是在看一些代码在哪里初始化一个整数

所有使用F的以下语句;


unsigned int i = -1;


我想知道这是否正确做这些事情?

如果没有,这样做的正确方法是什么?
>
I was just looking at some code where to initialize an integer
with all F''s following statement is used;

unsigned int i = -1;

I want to know if this is the right way of doing the things ?
if not, what is the correct way to do this ?



它不是一个整数,它是一个无符号整数。不一样。是的,

unsigned int将初始化为2 ** n - 1.如果" sizeof

unsigned int * CHAR_BIT"是8的倍数,这将是一个十六进制的
集合。


将unsigned int复制到int中通常会导致未定义

行为,因为它超出了int的范围。这种行为可能是你想要的,或者其他任何东西。你只是不知道。


-

Chuck F(cinefalconer at maineline dot net)

可供咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>


-

通过 http://www.teranews.com 上的免费Usenet帐户发布

It''s not an integer, it''s an unsigned int. Not the same. And yes,
the unsigned int will be initialized to 2**n - 1. If "sizeof
unsigned int * CHAR_BIT" is a multiple of 8, that will be a
collection of hex f.

Copying that unsigned int into an int will normally cause undefined
behaviour, because it out of range for an int. That behaviour may
be exactly what you want, or anything else. You just don''t know.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于用所有F初始化一个int变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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