(int&)a 的意思,其中a為float類型。

來源:互聯網
上載者:User

float a=1.0f;<br /> cout < <(int)a < <endl;<br /> cout < <&a < <endl;<br /> cout < <(int &)a < <endl;
其中的(int  &)a是什麼意思啊  ?

 

(int&)a 不經過轉換, 直接得到a在記憶體單元的值,並將其轉換成整數輸出。
(int)a a在記憶體中的值轉換成int類型

float類型在記憶體中儲存的形式是 ,符號位 指數 尾數
由754標準:階碼採用增碼(該數補碼的反符號),尾數採用原碼
所以1.0f 在記憶體中的形式為
0011 1111 1000 0000 0000 0000 0000 0000
所以輸出的是 0x3f800000

 

 

下面的函數更詳細的解釋了原理:

void ShowFloat32(float f)<br />{<br />unsigned int d = *(int*)&f;<br />cout << "符號:" << int((d&0x80000000)>>31) << '/t';<br />cout << "階碼:" << int((d&0x7f800000)>>23) << '/t';<br />cout << "尾數:" << int(d&0x007fffff) << endl;<br />for (unsigned int i = 31; i > 0; i--)<br />{<br />cout << int((d&(1 <<i))>>i);<br />if (i == 31 || i == 23) cout << ' ';<br />}<br />cout << endl;<br />}

 

說白了:(int&)a就是*(int*)&a的意思

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.