陳奕熹
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    2
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # C 語言強制轉型 (casting) ```c #include <stdio.h> int main() { unsigned int a = 10; double b = (double)a; printf("a = %u, b = %lf in decimal form\n", a, b); printf("a = %x, b = %x in hex form\n", a, b); } ``` 以上是一個簡單的 C 語言 casting,把無號數轉換成浮點數 ``` a = 10, b = 10.000000 in decimal form a = a, b = 15 in hex form ``` 可以看到儘管表示同一個數字,但是實際上在不同資料型態間二進位表示是完全不同的,這也是為什麼在 C 語言中我們需要透過 Casting 強制轉型才能讓一個數值在不同資料型態間轉換 > 不過你有想過,強制轉型底層是怎麼實做的嗎? ## `char` to `int` 與 `short` to `int` ```clike int main() { char c = 0x30; int b = (int) c; short s = 0x30; int a = (int) s; } ``` ``` push rbp mov rbp, rsp mov BYTE PTR [rbp-1], 48 movsx eax, BYTE PTR [rbp-1] mov DWORD PTR [rbp-8], eax mov WORD PTR [rbp-10], 48 movsx eax, WORD PTR [rbp-10] mov DWORD PTR [rbp-16], eax mov eax, 0 pop rbp ret ``` 1. 把原本只佔 1 BYTE 的`char` 形態用`int` Double WORD (4 BYTE) 的方法取出。 2. 把原本只佔 1 WORD 的`short` 形態用`int` Double WORD (4 BYTE) 的方法取出。 ## `int` to `float` > int 轉換成 float 就比較困難,float 因為遵守 IEEE 754 的規定雖然可以表示小數點但是數值的表示變得複雜 ```clike int main() { int a = -10; float b = (float) a; return 0; } ``` 下面是利用 godbolt 所產生的組合語言 ``` push rbp mov rbp, rsp mov DWORD PTR [rbp-4], -10 cvtsi2ss xmm0, DWORD PTR [rbp-4] movss DWORD PTR [rbp-8], xmm0 mov eax, 0 pop rbp ret ``` cvtsi2ss 即是把`int`轉換成`float`的 x86 指令,這個指令的全名是 > CVTSI2SS — Convert Doubleword Integer to Scalar Single-Precision Floating-Point Value > 也就是用一個指令來 convert。至於實作就看各家晶片製造商的做法。 xmm1 這個暫存器也很有趣,根據[維基百科](https://en.m.wikipedia.org/wiki/Streaming_SIMD_Extensions#Registers)的敘述,xmm1 是在 SSE ( Streaming SIMD Extensions ) 指令集的時候加入的暫存器,有 128 bits 可以進行單指令多資料的操作。 :::info 除了資料型態轉換可能會改變原始資料儲存方式,C 語言中的「型別修飾字」(qualifiers)會不會也會影響呢? ::: ## `int` to `unsigned int` ```c= int main() { int a = -10; unsigned int b = (unsigned int) a; printf("a=%x, b=%x\n", a, b); } ``` 事實上,這邊印出來的 hex 型態都是相同的 0xfffffff6 :::info 其實 `unsigned` 這樣的修飾字並不會影響資料數值,不過在讀取的時候對同一筆資料的解讀方式會不一樣 > C99 規格書裡面應該可以找到 reference,我是找 `type qualifier` 不過符合的量還是太多 [name=陳奕熹] ::: ## `int` and `const int` ```c int main() { int a = 10; const int b = 10; printf("a = %x, &a = %x\n", a, &a); printf("b = %x, &a = %x\n", b, &b); } ``` 十分意外的,這是編譯後的結果 > a = a, &a = 7fffffffdd90 > b = a, &a = 7fffffffdd94 > /* 用 gdb 看 stack pointer 在 7fffffffddb0 */ a, b在二進制時表現相同這點,從前面的 `unsigned` 部份就可以理解到二進制不會被改動 不過令人意外的是,被標記為 `const` 的資料並不會被存到下面 C語言記憶體配置圖中的 `text` 區域 ![](https://i.imgur.com/ds7FJbu.png) ```c int main() { int a = 10; const int b = (const int) a; } ``` ``` main: push rbp mov rbp, rsp mov DWORD PTR [rbp-4], 10 /* 從這邊開始是 line 4 */ mov eax, DWORD PTR [rbp-4] mov DWORD PTR [rbp-8], eax /* line 4 結束 */ mov eax, 0 pop rbp ret ``` 有趣的是,從編譯出來的組合語言可以知道,事實上 ```c int b = a; ``` ```c const int b = (unsigned int) a; ``` 兩者在完全沒做最佳化(-O0)時,編譯的結果就已經一樣了

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully