在此示例中,0xFFFFFFFF在做什么? [英] What is the 0xFFFFFFFF doing in this example?

查看:163
本文介绍了在此示例中,0xFFFFFFFF在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道arc4random返回一个无符号整数,最大为(2 ^ 32)-1.在这种情况下,它总是给出一个介于0和1之间的数字.

I understand that arc4random returns an unsigned integer up to (2^32)-1. In this scenario it it always gives a number between 0 and 1.

var x:UInt32 = (arc4random() / 0xFFFFFFFF)

如何用0xFFFFFFFF除以使数字在0到1之间?

How does the division by 0xFFFFFFFF cause the number to be between 0 - 1?

推荐答案

如您所述,

arc4random返回一个无符号整数,最大为(2 ^ 32)-1

arc4random returns an unsigned integer up to (2^32)-1

0xFFFFFFFF等于(2 ^ 32)-1,这是arc4random()的最大可能值.因此,算术表达式(arc4random() / 0xFFFFFFFF)给出的比率始终在0到1之间-由于这是整数除法,因此结果只能在0到1之间.

0xFFFFFFFF is equal to (2^32)-1, which is the largest possible value of arc4random(). So the arithmetic expression (arc4random() / 0xFFFFFFFF) gives you a ratio that is always between 0 and 1 — and as this is an integer division, the result can only be between 0 and 1.

这篇关于在此示例中,0xFFFFFFFF在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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