如何在两个32位整数组合成一个64位的整数。 [英] How to combine two 32-bit integers into one 64-bit integer?

查看:882
本文介绍了如何在两个32位整数组合成一个64位的整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计数寄存器,它是由两个32位的无符号整数,一个用于该值的高32位(最显著字),以及其他的值的低32位(至少显著字)。

什么是C到这两个32位无符号整数组合,然后显示大批的最佳方式?

在特定的:

  leastSignificantWord = 4294967295; // 2 ^ 32-1的printf(计数器:%U%U,mostSignificantWord,leastSignificantWord);

这将打印精细。

当数递增到4294967296,我有这样的leastSignificantWord湿巾为0,mostSignificantWord(0开始)现在1.全计数器现在应该读4294967296,但现在它只是显示的是10,因为我米距离leastSignificantWord串联1从mostSignificantWord和0。

我应该

如何使它显示4294967296,而不是10?


解决方案

 长长VAL =(久长)mostSignificantWord<< 32 | leastSignificantWord;
的printf(%LLI,VAL);

I have a count register, which is made up of two 32-bit unsigned integers, one for the higher 32 bits of the value (most significant word), and other for the lower 32 bits of the value (least significant word).

What is the best way in C to combine these two 32-bit unsigned integers and then display as a large number?

In specific:

leastSignificantWord = 4294967295; //2^32-1

printf("Counter: %u%u", mostSignificantWord,leastSignificantWord);

This would print fine.

When the number is incremented to 4294967296, I have it so the leastSignificantWord wipes to 0, and mostSignificantWord (0 initially) is now 1. The whole counter should now read 4294967296, but right now it just reads 10, because I'm just concatenating 1 from mostSignificantWord and 0 from leastSignificantWord.

How should I make it display 4294967296 instead of 10?

解决方案

long long val = (long long) mostSignificantWord << 32 | leastSignificantWord;
printf( "%lli", val );

这篇关于如何在两个32位整数组合成一个64位的整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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