twos-complement.html
1 <!DOCTYPE html> 2 <html lang="de"> 3 <head> 4 <meta charset="UTF-8" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 6 <meta http-equiv="onion-location" content="http://bopbopl6lohkl2rts3ltesjnag4hzs4jrx2h6k6etgq5xasbpqekzlqd.onion" /> 7 <title>BOP Wiki: Two's complement</title> 8 <link rel="stylesheet" href="/assets/stylesheet.css" /> 9 <link rel="icon" type="image/x-icon" href="/assets/img/favicon.png"> 10 </head> 11 <body> 12 <header> 13 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 14 <script src="/assets/js/navbar-OpenClose.js"></script> 15 <script src="/assets/js/lightbox.js"></script> 16 <script src="/assets/js/copyCodeButton.js"></script> 17 <link rel="stylesheet" href="/resources/js-libraries/highlightJS/atom-one-dark.min.css"> 18 <script src="/resources/js-libraries/highlightJS/highlight.min.js"></script> 19 <script src="/resources/js-libraries/highlightJS/highlightjs-line-numbers.min.js"></script> 20 <script>hljs.highlightAll();</script> 21 <script>hljs.initLineNumbersOnLoad();</script> 22 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 23 <div class="branding"> 24 <button class="toggle-btn-navbar" id="navbarOpenButton">☰</button> 25 <a href="/"> 26 <img class="logo" src="/assets/img/logo.png"> 27 </a> 28 <div class="typing-animation">BytesOfProgress</div> 29 </div> 30 </header> 31 <div id="navbarContainer" class="navbar-container"> 32 <iframe class="navbar-iframe" src="/assets/navbar/navbar.html" frameBorder= "0"></iframe> 33 </div> 34 <main> 35 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 36 37 <article class="site-post"> 38 <header class="post-header"> 39 <h1 class="post-title">Two's complement</h1> 40 <div class="post-meta"> 41 </div> 42 </header> 43 </article> 44 45 <nav class="breadcrumb"> 46 <a href="/">Home</a> 47 <span class="divider">›</span> 48 <a href="/wiki/">Wiki</a> 49 <span class="divider">›</span> 50 <a href="/wiki/math/math.html">Mathematics</a> 51 <span class="divider">›</span> 52 <span class="current">Two's complement</span> 53 </nav> 54 55 <section class="post-content"> 56 57 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 58 59 60 <p> 61 What do you need to know for this? 62 </p> 63 64 <p> 65 <a class="text-link" href="/wiki/math/binary-plus-minus/binary-plus-minus.html">Binary +/-</a> 66 </p> 67 68 <p> 69 Two's complement is a representation of negative numbers in binary 70 representation, often used in computer arithmetic. It achieves the 71 possibility of representing both positive and negative integers 72 in the binary system. It allows subtractions to be performed 73 as additions, which is very convenient for computer operations. 74 </p> 75 76 <p> 77 To find the two's complement representation of a negative number, first 78 take the binary representation of the positive number and then invert 79 each bit (1 becomes 0 and vice versa). 80 </p> 81 82 <p> 83 Then 1 is added to the inverted 84 number to get two's complement. 85 </p> 86 87 <p> 88 Example: 89 </p> 90 91 <p> 92 The binary representation of "5" is "0101". To find the two's complement 93 of "-5", we invert the bits, resulting in "1010", then we add "1" to 94 get "1011". 95 </p> 96 97 <p> 98 5 = 0101 99 </p> 100 101 <p> 102 Inverted: 1010 103 </p> 104 105 <p> 106 +1 = 1011 107 </p> 108 109 <span style="color:#ff6600"> 110 <h1 style="font-size:25px">Practical example with subtraction</h1> 111 </span> 112 113 <p> 114 239 - 224 115 </p> 116 117 <p> 118 239 = 11101111 119 </p> 120 121 <p> 122 224 = 11100000 123 </p> 124 125 <p> 126 224 Two's complement = 00100000 127 </p> 128 129 <p> 130 Now we add up "11101111" and "00100000": 131 </p> 132 133 <p> 134 Important: When using the two's complement, we do not use the transfer 135 of the last bit if there is one: 136 </p> 137 138 <img src="1.jpg"/> 139 140 <br> 141 142 <p> 143 00001111 = 15 144 </p> 145 146 <p> 147 239 - 224 = 15 148 </p> 149 150 151 152 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 153 154 </section> 155 <hr /> 156 <footer class="post-footer"> 157 <a href="/wiki/math/math.html" class="cta-button">← Back</a> 158 </footer> 159 </main> 160 </body> 161 </html>