不同 int 类型的运算

2024-03-24

我有一个使用多种不同 int 类型的程序。

最常用的是uint64_t和标准int。但是我想知道我是否可以安全地进行它们之间的混合操作。

例如我有一个uint64_t我想添加一个int并将该值存储为另一个值uint64_t.

做这样的事情安全吗?我必须投射int to uint64_t在我可以对其进行操作之前?

我在网上实在找不到有关它的东西。它可能只是被允许,没有人质疑它或者我的谷歌查询是错误的。

无论如何,基本上我的问题是我可以混合不同类型的整数并进行操作吗?


是的你可以。

您的编译器将负责转换。唯一需要担心的是溢出 - 如果您将结果存储在比输入小的容器中。

您需要的 Google 搜索词是“隐式类型转换” - 请参阅示例http://pic.dhe.ibm.com/infocenter/ratdevz/v8r5/index.jsp?topic=%2Fcom.ibm.tpf.toolkit.compilers.doc%2Fref%2Flangref_os390%2Fcbclr21011.htm http://pic.dhe.ibm.com/infocenter/ratdevz/v8r5/index.jsp?topic=%2Fcom.ibm.tpf.toolkit.compilers.doc%2Fref%2Flangref_os390%2Fcbclr21011.htm

该链接包括下表:

算术转换按以下顺序进行:

Operand Type                                  Conversion
---------------------------------------------+--------------------------------------------
One operand has long double type             | The other operand is converted to long double type.
---------------------------------------------+--------------------------------------------
One operand has double type                  | The other operand is converted to double.
---------------------------------------------+--------------------------------------------
One operand has float type                   | The other operand is converted to float.
---------------------------------------------+--------------------------------------------
One operand has unsigned long long int type  | The other operand is converted to unsigned long long int.
---------------------------------------------+--------------------------------------------
One operand has long long int type           | The other operand is converted to long long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned long int type       | The other operand is converted to unsigned long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type            |
and the other operand has long int type      |
and the value of the unsigned int can be     |
 represented in a long int                   | The operand with unsigned int type is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type            |
and the other operand has long int type      |
and the value of the unsigned int cannot be  |
represented in a long int                    | Both operands are converted to unsigned long int
---------------------------------------------+--------------------------------------------
One operand has long int type                | The other operand is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type            | The other operand is converted to unsigned int.
---------------------------------------------+--------------------------------------------
Both operands have int type                  | The result is type int.
---------------------------------------------+--------------------------------------------
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不同 int 类型的运算 的相关文章

随机推荐

  • 将 ereg_replace 替换为 preg_replace [重复]

    这个问题在这里已经有答案了 您好 需要更改功能ereg replace theData 到 preg replace To port ereg replace to preg replace你需要将正则表达式放在一对之间分隔符 你的正则表达
  • 特别分发:无法安装应用程序

    我正在开发一个ios应用程序 仅适用于iphone 并试图让其他人测试它 我通过临时分发在自己的设备上安装该应用程序没有问题 当我尝试在另一个人的设备上安装时 它总是失败 并显示消息 此时无法安装 xxxxx 我正在使用 xcode 5 以
  • Jython 不会将 PYTHONPATH 加载到 sys.path 中

    根据我读到的内容 sys path 应该由 PYTHONPATH 设置 在 Python 中 它是这样工作的 但在 Jython 中则不然 我可以绕过 Dpython path 但我想知道为什么 Jython 表现不佳 qa Scantro
  • 在 Visual Studio 2010 中使用正则表达式查找和替换来小写

    我正在使用 Visual Studio 2010 中的查找和替换功能来更改字段的编码样式 所有实例类似于 m MyField 应该 myField 但我只能设法得到 MyField using Find what m a zA Z Repl
  • 无法安装 sqlite3-ruby gem

    我已经安装了 xcode 3 0 我需要安装 sqlite3 ruby gem 用于 rhosync 当我跑步时 sudo gem install l sqlite3 ruby 我收到以下错误 Building native extensi
  • 重复的 typedef - 在 C 中无效但在 C++ 中有效?

    我想要一个标准参考 为什么以下代码会触发 C 中的合规性警告 使用gcc pedantic typedef 重新定义 但在 C 中没问题 g pedantic typedef struct Foo Foo typedef struct Fo
  • 如何判断文件是否为空?

    如何判断文件是否为空 该文件由运行在Windows平台上的C程序打开 我想以附加模式打开一个文件 如果为空 首先打印一个标题 Open CSV write header report csv fopen SNR csv a if repor
  • 在 Mac 上解压缩大型 ZIP 档案(例如最新的 Microsoft Edge VM)

    尝试在我的 Mac 上解压最新的 Microsoft Edge Vm zip 文件后 我最终得到一个 cpgz 文件 它应该为 virtualBox 生成一个 ova 文件 有人知道怎么修这个东西吗 http mattormeeple co
  • 何时使用 handler.post() 以及何时使用 new Thread()

    我想知道什么时候应该使用handler post runnable 我什么时候应该使用new Thread runnable start Handler 的开发人员文档中提到了这一点 导致 Runnable r 添加到消息队列中 可运行的
  • MS Word 在 python 中的读/写、Python-docx 问题和 win32com 参考?

    最近 我正在尝试使用不同的 API 进行 MS Word 文件管理 现在正在编写 此时我只需要一个简单的编写Python API 我尝试了 win32com 模块 事实证明该模块非常强大 但缺乏 python 在线示例 对 VB 和 C 的
  • nginx 位置正则表达式 - 字符类和匹配范围

    我正在尝试为路径设置正则表达式 s lt 4 6 character string here gt 我将 4 6 个字符串捕获为 1 我尝试使用以下两个条目 但都失败了 location s 0 9a zA Z 4 6 location s
  • 不同Y级别的UIButton无法在tvOS上聚焦

    我开始为 Apple TV 开发应用程序 但在按钮方面遇到了这个问题 我有一个屏幕 屏幕底部有几个按钮 中间有一个按钮 请参阅屏幕截图以了解 问题是我无法集中中间按钮 焦点仅位于底部的三个按钮上 问题显然是由Y位置 当我移动屏幕底部的按钮时
  • 命令未存储在命令历史记录中

    背景 https stackoverflow com questions 76566358 这个答案 https stackoverflow com a 47595405 2153235依赖于 readline 模块并且write hist
  • 在 MVC razor 视图中的 foreach 循环中对单选按钮进行分组?

    我尝试通过在 html 帮助中提供额外的 html 属性来对循环内的一组单选按钮进行分组 如下所示 ol class Opt foreach var opt in quest Options li class Opt Html RadioB
  • 从 git 包中恢复所有引用

    如何从备份恢复所有远程分支 git bundle Backup git bundle create tmp dp all git bundle list heads tmp dp head n5 f37c9fc7f0ce121568f42f
  • 改进低效的 jQuery 选择器

    在 IntelliJ 中 如果我使用 jQuery 选择器 例如 roleField option each function impl omitted 选择器突出显示 并建议我应该 以 ID 选择器开头的拆分后代选择器 IntelliJ
  • 语音训练文件和注册表位置

    我有一个演讲项目 需要用代码完成声学训练 我能够使用 SAPI 在 Windows 7 下成功创建包含成绩单及其关联注册表项的培训文件 但是 我无法确定识别引擎是否成功使用这些文件并调整其模型 我的问题如下 通过控制面板训练 UI 执行训练
  • Cordova InAppBrowser 不会缩放加载的页面

    谁能帮我获取 Cordova InAppBrowser 在 Android 应用程序上加载的外部页面以适合手机屏幕尺寸 我使用以下代码从 Sencha Touch 应用程序调用 inappbrowser var opt location n
  • AngularJS 'scrollTop' 等效吗?

    我希望在 AngularJS 指令中实现类似的东西 https github com geniuscarrier scrollToTop blob master jquery scrollToTop js https github com
  • 不同 int 类型的运算

    我有一个使用多种不同 int 类型的程序 最常用的是uint64 t和标准int 但是我想知道我是否可以安全地进行它们之间的混合操作 例如我有一个uint64 t我想添加一个int并将该值存储为另一个值uint64 t 做这样的事情安全吗