您好,登錄后才能下訂單哦!
void Test()
{
//原則
//字節相同,只作二進制拷貝
//2字節->4字節,根據符號性先將2字節的數據轉換成對應的4字節
//4字節->2字節,直接拷貝低位
int i = 0;
{
//INT16 -> UINT16
//UINT16 -> UINT16
}
{
//INT32 -> UINT32
INT32 n1 = 0xffffffff;
UINT32 u1 = n1;//0xffffffff
++i;
}
{
//UINT32 -> INT32
UINT32 u1 = 0xffffffff;
INT32 n1 = u1;//0xffffffff
++i;
}
{
//INT16 -> INT32
INT16 n1 = 0xffff;
INT32 n2 = n1; //0xffffffff, 2字節擴展到4字節,符號保持不變
++i;
}
{
//INT16 -> UINT32
INT16 n1 = 0xffff;
UINT32 u1 = n1; //0xffffffff, INT16->INT32->UINT32
u1 = (UINT32)(n1&0x0000ffff); //0xffff
n1 = 0x1;
u1 = n1;//0x1
++i;
}
{
//UINT16 -> UINT32
UINT16 u1 = 0xffff;
UINT32 u2 = u1;//0x0000ffff,高位補0
++i;
}
{
//UINT16 -> INT32
UINT16 u1 = 0xffff;
INT32 n1 = u1;//0xffff, UINT16 -> UINT32 -> INT32
++i;
}
{
//INT32 -> INT16
INT32 n1 = 0xf0001000;
INT16 n2 = n1; //0x1000,直接截斷,取低2字節
//INT32 -> UINT16
n1 = 0xf1234567;
UINT16 u1 = n1; //0x4567,直接截斷,取低2字節
++i;
}
{
//UINT32 -> INT16
UINT32 u1 = 0xf123f456;
INT16 n1 = u1; //0x4567,直接截斷,取低2字節
//UINT32 -> UINT16
UINT16 u2 = u1; //0x4567,直接截斷,取低2字節
++i;
}
}
void Test2()
{
// UINT64 u1 = 0x1200003456000078;
// UINT32 u2= UINT32(u1&(UINT64)0x00000000ffffffff);
// UINT32 u3= UINT32(u1>>32);
// printf("UINT64 is: %0.16llx\n", u1);
// printf("UINT32 low: %0.8x\n", u2);
// printf("UINT32 hi: %0.8x\n", u3);
//%d需要一個INT32(如果不足4字節,轉成4字節),轉成INT32,而不關心變量本身的數據類型
//%u需要一個UIN32(如果不足4字節,轉成4字節),轉成UINT32,而不關心變量本身的數據類型
{
//%d/%u + INT32
INT32 n1 = 0xffffffff;
TEA_LOG(LOG_DEBUG, _T("INT32 %d, %u"), n1, n1);
//INT32 -1, 4294967295
}
{
//%d/%u + UINT32
UINT32 u1 = 0xffffffff;
TEA_LOG(LOG_DEBUG, _T("UINT32 %d, %u"), u1, u1);
//UINT32 -1, 4294967295
}
{
//%d/%u + INT16
INT16 n1 = 0xffff;
TEA_LOG(LOG_DEBUG, _T("INT16 %d, %u"), n1, n1);
//INT16 -1, 4294967295
//INT16 -> INT32 -> UINT32
}
{
//%d/%u + UINT16
UINT16 u1 = 0xffffffff;
TEA_LOG(LOG_DEBUG, _T("UINT16 %d, %u"), u1, u1);
//UINT16 65535, 65535
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。