From 7fee8fa46e7a49595d3bc5cb7f49c001d5e91c55 Mon Sep 17 00:00:00 2001 From: anebz Date: Tue, 11 Feb 2020 10:37:31 +0100 Subject: [PATCH] 5. bit manipulation readme v1 --- 05. Bit manipulation/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 05. Bit manipulation/README.md diff --git a/05. Bit manipulation/README.md b/05. Bit manipulation/README.md new file mode 100644 index 0000000..25c4c01 --- /dev/null +++ b/05. Bit manipulation/README.md @@ -0,0 +1,29 @@ +# Chapter 5. Bit manipulation + +## 5.1. Two's complement and negative numbers + +Positive numbers are represented as themselves, negative numbers as the two's complement of its absolute value, with a 1 in its sign bit, indicating it's negative. + +The two's complement of an N-bit number (N: \# bits used for the number, excluding the sign bit), is the complement of the number with respect to 2N. + +The binary representation of -K as a N-bit number is concat(1, 2N-1 - K). + +## 5.2. Arithmetic vs. Logical right shift + +Arithmetic right shift: division by two. Logical right shift: 'shifting the bits'. + +### 5.2.1. Logical shift + +Indicated with a >>> operator, all bits are shifted, sign bit too. + +* -75: **1**0110101 +* 90: **0**1011010 + +### 5.2.2. Arithmetic shift + +The sign bit is kept, and all bits (sign bit too) are shifted. + +* -75: **1**0110101 +* -38: **1**1011010 + +## 5.3. Common bit tasks: getting and setting \ No newline at end of file