uint64_t的运算出错问题

rickys2080 2013-10-09 09:46:55
各位大神请看下面的程序,我的问题在注释里

//编译环境:codeblocks+gcc
#include <stdio.h>
#include <stdint.h>
int Fun()
{
uint64_t y;
uint32_t x1, x2;

y = 3000 * 24000000 / 1000;
printf("0x%llX\n", y);//输出错误结果:0xFFFFFFFFFFF08554 ,Why??? int64位不能正确运算吗?
x1 = (uint32_t)(y & 0xFFFFFFFF);
x2 = (uint32_t)(y >> 32);
printf("0x%X,0x%X \n", x1, x2);//输出错误结果:0xFFF08554, 0xFFFFFFFF

y = 3000 * (24000000 / 1000);
printf("0x%llX\n", y);//输出正确结果: 0x44AA200
x1 = (uint32_t)(y & 0xFFFFFFFF);
x2 = (uint32_t)(y >> 32);
printf("0x%X,0x%X \n", x1, x2);//正确结果 :0x44AA200, 0x0
}
int main()
{
Fun();
}
...全文
686 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-10-09
  • 打赏
  • 举报
回复
y = 3000ui64 * 24000000ui64 / 1000ui64;
C/C++语言整数常量语法(参考3楼)不过关。
rickys2080 2013-10-09
  • 打赏
  • 举报
回复
楼上都是专家啊,学习了,谢谢!
qq120848369 2013-10-09
  • 打赏
  • 举报
回复
y = (uint64_t)3000 * (uint64_t)24000000 / 1000; C语言基础不过关。
赵4老师 2013-10-09
  • 打赏
  • 举报
回复
3000和3000i64和3000ui64不是一回事!
赵4老师 2013-10-09
  • 打赏
  • 举报
回复
常量也有类型! C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Syntax integer-constant : decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant : nonzero-digit decimal-constant digit octal-constant : 0 octal-constant octal-digit hexadecimal-constant : 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit : one of 1 2 3 4 5 6 7 8 9 octal-digit : one of 0 1 2 3 4 5 6 7 hexadecimal-digit : one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix : unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix : one of u U long-suffix : one of l L 64-bit integer-suffix : i64 To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value
rickys2080 2013-10-09
  • 打赏
  • 举报
回复
引用 1 楼 turingo 的回复:
y = 3000 * 24000000 / 1000; 溢出了。
y的定义是uint64_t y; 64位的,怎么就溢出了?
图灵狗 2013-10-09
  • 打赏
  • 举报
回复
y = 3000 * 24000000 / 1000; 溢出了。
引用 楼主 syrchina 的回复:
各位大神请看下面的程序,我的问题在注释里

//编译环境:codeblocks+gcc 
#include <stdio.h>
#include <stdint.h>
int Fun()
{
	uint64_t y;
	uint32_t x1, x2;

    y = 3000 * 24000000 / 1000;
	printf("0x%llX\n", y);//输出错误结果:0xFFFFFFFFFFF08554 ,Why??? int64位不能正确运算吗? 
	x1 = (uint32_t)(y & 0xFFFFFFFF);
    x2 = (uint32_t)(y >> 32);
    printf("0x%X,0x%X \n", x1, x2);//输出错误结果:0xFFF08554, 0xFFFFFFFF
	
    y = 3000 * (24000000 / 1000);
	printf("0x%llX\n", y);//输出正确结果: 0x44AA200
	x1 = (uint32_t)(y & 0xFFFFFFFF);
    x2 = (uint32_t)(y >> 32);
    printf("0x%X,0x%X \n", x1, x2);//正确结果 :0x44AA200, 0x0 
}
int main()
{
	Fun();
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧