diff --git a/Gemfile.lock b/Gemfile.lock index ecb9f7b..feb7548 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GEM remote: https://rubygems.org/ specs: - activesupport (8.1.0) + activesupport (8.1.1) base64 bigdecimal concurrent-ruby (~> 1.0, >= 1.3.1) @@ -14,8 +14,8 @@ GEM securerandom (>= 0.3) tzinfo (~> 2.0, >= 2.0.5) uri (>= 0.13.1) - addressable (2.8.7) - public_suffix (>= 2.0.2, < 7.0) + addressable (2.8.8) + public_suffix (>= 2.0.2, < 8.0) base64 (0.3.0) bigdecimal (3.3.1) coffee-script (2.4.1) @@ -25,9 +25,9 @@ GEM colorator (1.1.0) commonmarker (0.23.12) concurrent-ruby (1.3.5) - connection_pool (2.5.4) + connection_pool (3.0.2) csv (3.3.5) - dnsruby (1.73.0) + dnsruby (1.73.1) base64 (>= 0.2) logger (~> 1.6) simpleidn (~> 0.2.1) @@ -35,16 +35,17 @@ GEM em-websocket (0.5.3) eventmachine (>= 0.12.9) http_parser.rb (~> 0) - ethon (0.17.0) + ethon (0.18.0) ffi (>= 1.15.0) + logger eventmachine (1.2.7) execjs (2.10.0) faraday (2.14.0) faraday-net_http (>= 2.0, < 3.5) json logger - faraday-net_http (3.4.1) - net-http (>= 0.5.0) + faraday-net_http (3.4.2) + net-http (~> 0.5) ffi (1.17.2-x64-mingw-ucrt) ffi (1.17.2-x86_64-linux-gnu) forwardable-extended (2.6.0) @@ -217,7 +218,7 @@ GEM gemoji (>= 3, < 5) html-pipeline (~> 2.2) jekyll (>= 3.0, < 5.0) - json (2.15.1) + json (2.17.1) kramdown (2.4.0) rexml kramdown-parser-gfm (1.1.0) @@ -232,9 +233,9 @@ GEM jekyll (>= 3.5, < 5.0) jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) - minitest (5.26.0) - net-http (0.6.0) - uri + minitest (5.26.2) + net-http (0.8.0) + uri (>= 0.11.1) nokogiri (1.18.10-x64-mingw-ucrt) racc (~> 1.4) nokogiri (1.18.10-x86_64-linux-gnu) @@ -258,7 +259,7 @@ GEM sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sawyer (0.9.2) + sawyer (0.9.3) addressable (>= 2.3.5) faraday (>= 0.17.3, < 3) securerandom (0.4.1) @@ -269,12 +270,9 @@ GEM ethon (>= 0.9.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - tzinfo-data (1.2025.2) - tzinfo (>= 1.0.0) unicode-display_width (1.8.0) - uri (1.0.4) - wdm (0.1.1) - webrick (1.9.1) + uri (1.1.1) + webrick (1.9.2) PLATFORMS x64-mingw-ucrt diff --git a/README.md b/README.md index bedf265..6c28c84 100644 --- a/README.md +++ b/README.md @@ -10,24 +10,23 @@ Click [here](https://de-soot.github.io) ### Run Locally -1) [Clone](https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories) this github repository onto your computer by simply downloading the source code from this Github repository, or by using [`git`](https://git-scm.com): +1. [Clone](https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories) this github repository onto your computer by simply downloading the source code from this Github repository, or by using [`git`](https://git-scm.com): ```sh git clone https://github.com/de-soot/de-soot.github.io ``` -2) Follow [this guide](https://jekyllrb.com/docs/installation) to install the prerequisites for Jekyll for your system if you have not already done so. -3) Once all requirements are installed, run the following command below in a command line terminal: +2. Follow [this guide](https://jekyllrb.com/docs/installation) to install the prerequisites for Jekyll for your system if you have not already done so. +3. Once all requirements are installed, run the following command below in a command line terminal: ```sh gem install jekyll bundler ``` -4) Now that everything's installed and good to go, navigate the terminal to the directory where the you downloaded the copy of the Github repository, like so: +4. Now that everything's installed, navigate the terminal to the directory of the copy of the Github repository: ```sh cd [Path of source code folder] ``` -5) Enter this command: +5. Enter this command to preview the site: ```sh bundle exec jekyll serve ``` -6) Navigate to http://localhost:4000 in a browser. -7) Profit! - -Refer to the [official Jekyll documentation](https://jekyllrb.com/docs) for more information regarding installing and running Jekyll. +6. Navigate to http://localhost:4000 in a browser. +7. Add your own Markdown posts inside `_posts`. +8. Profit! diff --git a/_config.yml b/_config.yml index 7465846..3d69376 100644 --- a/_config.yml +++ b/_config.yml @@ -19,8 +19,8 @@ # in the templates via {{ site.myvariable }}. title: de_soot -github_username: de-soot -description: Hosted on Github Pages +github_username: de-soot +description: Technical blog baseurl: "" # the subpath of your site, e.g. /blog url: "https://de-soot.github.io" # the base hostname & protocol for your site, e.g. http://example.com domain: de-soot.github.io diff --git a/_posts/2025-02-20-groff-apa.md b/_posts/2025-02-20-groff-apa.md index 0f04311..719b853 100644 --- a/_posts/2025-02-20-groff-apa.md +++ b/_posts/2025-02-20-groff-apa.md @@ -1,7 +1,7 @@ --- layout: post title: Guide to APA with groff ms and refer -date: 2025-02-07 +date: 2025-02-20 categories: groff, ms, refer, macro, tmac, APA, style guide, pdf permalink: /groff-apa --- diff --git a/_posts/2025-12-08-2s-complement.md b/_posts/2025-12-08-2s-complement.md new file mode 100644 index 0000000..5a4aace --- /dev/null +++ b/_posts/2025-12-08-2s-complement.md @@ -0,0 +1,38 @@ +--- +layout: post +title: 2's Complement Derivation +date: 2025-12-08 +categories: cs, binary, bits, negative numbers, derivation, proof, math, 2's complement +permalink: /2s-complement +--- + +If you have ever taken CS in high school or college, you have probably been taught about how to represent negative numbers in binary using 2's complement. + +But teachers generally just tell you to "flip the bits and add one" without really explaining why it works---similar to how high school teachers treat math formulas. + +I have never liked memorising without understanding when it comes to learning, so I came up with an intuitive derivation and wrote this blog post to share it in hopes that somebody may find it useful. + +## How Does 2's Complement Work? + +Say there is a number `x` represented by `n` binary digits (bits). +Since each bit can be `0` or `1`, there are 2^n possible combinations for the string of bits that represent values ranging from zero to 2^n - 1, and thus `x` is always less than 2^n. + +If we flip all the bits of `x` using the bitwise NOT operator (`~`), we get a new number with `1` bits where there were `0`'s in `x` and `0` bits where there were `1`'s in `x`. +We will call this new number `~x`, where `x + ~x = 2^n - 1`, since 2^n - 1 has all `n` number of bits set to `1`. + +Rearranging the above equation for `x`, we get `x = (2^n - 1) - ~x`. +It is then easy to find that `-x = -(2^n - 1 - ~x) = -2^n + 1 + ~x`. +Now see the picture of where "flip the bits and add one" comes from. + +But we cannot just flip bits and add 1 to get `-x` with ordinary bits, that would just be `~x + 1`. +We are still missing the `-2^n` in the equation, but how would we get that? + +Currently, since the value of the most significant bit is `2^(n-1)`, if we add another bit from the left, its place value would be `2^(n-1) * 2 = 2^n`. +Now all we have to do is define the value of that new bit to be negative to get `-2^n`. +When we flip the bits of `x`, this new bit we added will also get flipped along with it. + +## Conclusion + +Putting all the pieces together, we get that for a number `x` represented by `n` bits (with an extra bit having a place value of `-2^n`) where `x = (2^n - 1) - ~x`, you would need to flip its bits and add one to get `-2^n + ~x + 1 = -x`. + +Hopefully after reading this, you will have gained a better understanding of using 2's complement for converting binary numbers to negative. diff --git a/_site/404.html b/_site/404.html index 18e00d1..08f89c2 100644 --- a/_site/404.html +++ b/_site/404.html @@ -3,12 +3,12 @@ -
But teachers generally just tell you to “flip the bits and add one” without really explaining why it works—similar to how high school teachers treat math formulas.
+ +I have never liked memorising without understanding when it comes to learning, so I came up with an intuitive derivation and wrote this blog post to share it in hopes that somebody may find it useful.
+ +Say there is a number x represented by n binary digits (bits).
+Since each bit can be 0 or 1, there are 2^n possible combinations for the string of bits that represent values ranging from zero to 2^n - 1, and thus x is always less than 2^n.
If we flip all the bits of x using the bitwise NOT operator (~), we get a new number with 1 bits where there were 0’s in x and 0 bits where there were 1’s in x.
+We will call this new number ~x, where x + ~x = 2^n - 1, since 2^n - 1 has all n number of bits set to 1.
Rearranging the above equation for x, we get x = (2^n - 1) - ~x.
+It is then easy to find that -x = -(2^n - 1 - ~x) = -2^n + 1 + ~x.
+Now see the picture of where “flip the bits and add one” comes from.
But we cannot just flip bits and add 1 to get -x with ordinary bits, that would just be ~x + 1.
+We are still missing the -2^n in the equation, but how would we get that?
Currently, since the value of the most significant bit is 2^(n-1), if we add another bit from the left, its place value would be 2^(n-1) * 2 = 2^n.
+Now all we have to do is define the value of that new bit to be negative to get -2^n.
+When we flip the bits of x, this new bit we added will also get flipped along with it.
Putting all the pieces together, we get that for a number x represented by n bits (with an extra bit having a place value of -2^n) where x = (2^n - 1) - ~x, you would need to flip its bits and add one to get -2^n + ~x + 1 = -x.
Hopefully after reading this, you will have gained a better understanding of using 2’s complement for converting binary numbers to negative.
]]>