2013-08-07 24 views
0

我有兩個UINT16值轉換2個UINT16值到一個UInt32的值考慮至少和最顯著字

private UInt16 leastSignificantWord; 
private UInt16 mostSignificantWord; 

這兩個詞(UINT16值)來自其將一個UInt32的狀態/錯誤值成兩個組件單詞並返回這兩個單詞。現在我需要回到UInt32的值。總結這兩個詞在一起不會做到這一點,因爲如果最重要和最不重要的話是無視的。

例如:

private UInt16 leastSignificantWord = 1; 
private UInt16 mostSignificantWord = 1; 

//result contains the value 2 after sum both words 
//which can not be correct because we have to take note of the most and least significant 
UInt32 result = leastSignificantWord + mostSignificantWord; 

有沒有辦法解決這個問題的方法嗎?說實話,我從來沒有在C#中的位/字節工作,所以我從來沒有遇到過這樣的問題。在此先感謝

回答

3
private UInt16 leastSignificantWord = 1; 
private UInt16 mostSignificantWord = 1; 

UInt32 result = (leastSignificantWord << 16) + mostSignificantWord; 

你有2 UINT16(16位和16位) 一個0010 1011 1010 1110和第二1001 0111 0100 0110

如果你願意讀這2 UIn16作爲一個UInt32的你將有0010 1011 1010 1110 1001 0111 0100 0110

所以,(leastSignificantWord << 16)給你0010 1011 1010 1110 0000 0000 0000 0000這個加上mostSignificantWord給你0010 1011 1010 1110 1001 0111 0100 0110

這些可能有幫助

http://msdn.microsoft.com/en-us/library/a1sway8w.aspx

What are bitwise shift (bit-shift) operators and how do they work?

相關問題