陳家年
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 2018q3 Homework5 (bits) ## absVal 參考之前考試題目,由於有限制 operator `-` 。那其實我們原本在考試時候是用 `-y` ,換個想法就是把一個數取負號,不就可以利用二補數的概念 先取 ==1 補數再 +1== ,就可以拿到自己相反的數。 ```c= int absVal(int x) { int y = x >> 31; // get signed bit return (y ^ x)+(~y + 1); } ``` ## addOK 我們需要關心的是`正數+正數` `負數+負數` 造成 signed bit 變號, 所以我都取`signed bit`來做檢查,最後的 return 是用 Karnaugh Map 寫出來的。`正數+負數` `正數+負數` 可以放心的加就算 overflow 最後出來也是正確的。 ```c= int addOK(int x, int y) { int ans_sig = !!((x+y) >> 31); int x_sig = !!(x >> 31); int y_sig = !!(y >> 31); return ( (!x_sig & !ans_sig) | (y_sig & ans_sig) | (x_sig & !y_sig)); } ``` ## allEvenBits 想法就是跟`1010 1010 1010 1010 1010 1010 1010 1010 ` 結合一下,最後只要是 -1 ,就代表偶數位都是`1` ```c= int allEvenBits(int x) { return !((x | 0xAAAAAAAA )+1); } ``` ## allOddBits ```c= int allOddBits(int x) { return !((x | 0x55555555 )+1); } ``` >下面分數怪怪der,到底有沒有得分 >[name=jn] >[color=blue] ``` mec@mec-MS-7A63:~/JN_work/embededHW/datalab$ ./btest Score Rating Errors Function 4 4 0 absVal 3 3 0 addOK 2 2 0 allEvenBits 2 2 0 allOddBits ERROR: Test anyEvenBit(-2147483648[0x80000000]) failed... ...Gives 42[0x2a]. Should be 0[0x0] ERROR: Test anyOddBit(-2147483648[0x80000000]) failed... ...Gives 42[0x2a]. Should be 1[0x1] ``` ``` mec@mec-MS-7A63:~/JN_work/embededHW/datalab$ ./btest -f alloddBits Score Rating Errors Function Total points: 0/0 ``` ## anyEvenBit * 與 `0101 0101 0101 0101 0101 0101 0101 0101` 結合,利用`xor`有變化就知道有偶數 bit ```c= int anyEvenBit(int x) { return !!(x & 0x55555555); } ``` ## anyOddBit * 同樣概念 ```c= int anyOddBit(int x) { return !!(x & 0xAAAAAAAA); } ``` ## bang ```c= int bang(int x) { int or_sum = x | (~x+1); return 0x1 ^ (0x1 & (or_sum >> 31)); } ``` * 參考這裡的[作法](https://www.viseator.com/2017/06/18/CS_APP_DataLab/),就是利用與自己的相反數做 `or` 就可以透過最高位元知道這個數是不是`0`,後續就是做一些簡單取值的動做。 ## h2 bitAnd ```c= int bitAnd(int x, int y) { return ~(~x | ~ y); } ``` * 利用 `or` 特性一定要都是 `0` 才會是 `0` ## distinctNegation ```c= int distinctNegation(int x) { int x_ops = ~x+1; x = x_ops | x; x = x << 1 ; return !!x; } ``` * 我觀察三種狀況,假設只有 3 bits ,觀察當自己與自己的相反數做 `or` ,會有什麼結果。 * 011 | 101 * 000 | 000 * 100 | 100 ## getByte 由輸入來決定要 shift 幾個 bits ```c= int getByte(int x, int n) { return (x >> (n<<3)) & 0xff; } ``` ## implication -> A為真,則B也為真;如果A為假,則對B沒有任何影響 +|0|1| 0|1|1| 1|0|1| ```c= int implication(int x, int y) { return (!x )| y; } ``` ## isEqual 畫出 truth table ,即可了解用 XOR 配合驚探號 ```c= int isEqual(int x, int y) { return !(~(~x & ~y) & (~(x & y))); } ``` ## isGreater 畫出 truth table 即可了解。找出 x_sig, y_sig 與 sub_sig 找出之間的關係。因為是要 `>0` 不是 `>=0` , 所以再做相反數時候不用 `+1` ```c= int isGreater(int x, int y) { int sub_sign = !!((x+(~y))>>31); int x_sign = !!(x>>31); int y_sign = !!(y>>31); return ((!x_sign)&(!sub_sign)) | ((!x_sign)&y_sign) | (x_sign&y_sign&(!sub_sign)); } ``` ## isLess 把上面那題的 x 與 y 互換。 ```c= int isLess(int x, int y) { int sub_sign = !!((~x+(y))>>31); int x_sign = !!(x>>31); int y_sign = !!(y>>31); return ((!y_sign)&(!sub_sign)) | ((!y_sign)&x_sign) | (y_sign&x_sign&(!sub_sign)); } ``` ## isLessOrEqual 把上面那題的 `1` 加回去,奇怪的是單獨測試這個 function 盡然是 error function ,但是全部測試會過 operator 數目沒有超過(22個),也沒使用違規 operator ```c= int isLessOrEqual(int x, int y) { int sub_sign = !!(((~x+1)+y)>>31); int x_sign = !!(x>>31); int y_sign = !!(y>>31); return ((!y_sign)&(!sub_sign)) | ((!y_sign)&x_sign) | (y_sign&x_sign&(!sub_sign)); return 42; } ``` ## isNonZero 這個四分太好賺了吧~~~~LOL~~~ ```= /* * isNonZero - Check whether x is nonzero using * the legal operators except ! * Examples: isNonZero(3) = 1, isNonZero(0) = 0 * Legal ops: ~ & ^ | + << >> * Max ops: 10 * Rating: 4 */ int isNonZero(int x) { return !!x; } ``` ## isTmax 兩個條件 * 是不是正數 * 是不是0x7fffffff ```c= int isTmax(int x) { return (!(x+0x80000001))&(!(x>>31)); } ``` ## isTmin 同上概念 ```c= int isTmin(int x) { return (!(x+0x80000000))&(x>>31); } ``` ## isZero ```c= int isZero(int x) { return !x; } ``` ## greatestBitPos * [參考](https://stackoverflow.com/questions/21413565/create-a-mask-that-marks-the-most-significant-set-bit-using-only-bitwise-operat) ```c= int greatestBitPos(int x) { unsigned int m; m = x; m = m | m >> 1; m = m | m >> 2; m = m | m >> 4; m = m | m >> 8; m = m | m >> 16; m = m & ((~m >> 1)^0x80000000); return m; } ```

    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