Two’s Complement (Negative numbers in binary)

Honestly js watch this video :p

Convert numbers to and from 2’s Complement to a normal binary, is rly easy.

  • Step 1: invert ur binary number (apply bitwise NOT)
  • Step 2: add 1

Let’s try, Say you have the number -5, you want to express this as a binary.

  • Get the binary form of positive 5 → 0101 (We have a leading zero as that is the SIGN bit!!)
  • Invert the binary → 1010
  • Add 1 → 1011

“1011” is the 2’s Complement representation of -5!!

And in reverse:

  • Invert “1011”→ 0100
  • Add 1 → 0101

yuhhhhh

Signed & Unsigned Integers

Hey man it’s not deep.

If it’s unsigned, all bits are used to store the number:

  • 0d1111 = 15
  • 0d0101 = 5

If it’s signed, the most significant bit denotes the sign(+/-):

  • 0d1111 = -1
  • 0d0101 = 5

Remember, binary negative numbers are always written in two’s complements form

This calls back to the idea of datatypes. A series of bits can mean anything, depending on what we assume it to represent! So “0b1111” can be very different numbers depending on whether we think it’s an unsigned or signed integer.

BITWISE OPERATORS

OperatorSymbol
AND&
OR|
NOT~
XOR^
Shift Left<<
Shift Right>>
Assume shift to be arithmetic shift unless otherwise stated

They work by comparing the bits of 2 binary numbers!

Example: 0b1010 AND 0b1001

AND (&)
0b1010 →1010
0b1001 →1001
Output1010

SAL/SAR vs SHL/SHR

Shift Arithmetic Left (SAL) vs Shift Left (SHL)

They’re the same. They both shift a given set of binary numbers to the left, replacing the void with 0s

Shift Arithmetic Right (SAR) vs Shift Right (SHR)

SAR preserves the most significant bit (sign bit) of the binary number as it right shifts. As such, SAR is primarily meant to be used with signed integers, but of course can be used with any kind of binary data.

Whereas, SHR js moves shit to the right n replaces the empty spaces with 0

So, if the binary number is 0b0111, both SAR and SHR will do the same thing.

But, if the number is 0b1010:

Okay… But the f. why? What’s the point

Well, I’m glad u asked. First let’s look at right shifting decimal numbers. So we have the number 500 and we right shift it by 1. We get 50! Right shifting decimal numbers basically divides it by 10!

That’s the same with binary! right shifting divides by 2 and left shifting multiplies by 2!

So say, we want to use right shift to quickly divide a negative binary number by 2. SHR will corrupt the binary as it will fail to preserve the sign bit! But SAR on the other hand will do it just fine.

Example: Given the two’s complement binary “1100” which is “-4” in decimal. Here are the following outputs of SAR and SHR after shifting once:

  • SHR → “0110”: This is no longer a two’s complement number! if read as an integer it’s 6! useless!
  • SAR → “1110”: This is two’s complement for -2!!! Success!!!!!

Note: Left shifting a two’s complement number with SAL does not multiply it. WHy? try it out! And think about what it has to do to instead, to make it multiply. At that point, it’s no longer just a left shift!!

Whatt in the fuckk were those circuits????

Prof was just trying to give ya’ll the appreciation of the magic that is computing. this shit is unimportant

Those 3 legged things were transistors! Which are just on/off switches, but in order switch a transistor on, you will have to power it! So u can arrange many transistors in various patterns to on/off each other and that’s at a very basic level how a CPU works!!

BASE is where u feed current to connect the collector to emitter (switch on!)

image stolen from: https://technologystudent.com/elec1/transis1.htm

As you saw, prof arranged the transistors differently and created the following gates:

  • NOT: By reading the current at the collector
  • AND: By linking 2 transistors in series (collector to emitter)
  • NOR: By linking 2 transistors in parallel and reading from the collector

By combining NOT, AND and OR gates in a specific way we can get the XOR gate. With all these gates, like shown in the slide, we will be able to do proper arithmetic addition of binary numbers with carrying digits and all that!!! From there, smart people have made rocks think!! p cool stuffs

things I will check w prof n update ya’ll abt

  • is paper allowed for test 1?
    • Yesssss! Scrap paper is allowed!
  • How will I know if an integer is signed or unsigned in the test???
    • The question will explicitly mention!
  • How to know if I should do a arithmetic or unsigned right shift??
    • The question should state!!! If not, raise ur hand and ask!

hi this is for those who are unsure what a VM is

Assumptions:

  • You know what an operating system is.
  • You roughly know about basic computer hardware: CPU, HardDrive, SSD, RAM…
  • If you don’t, no worries. pls drop me a text and i’ll send u some resources 2 catch u up

Disclaimer

This is going to be a very basic overview

Virtual Machine

So a virtual machine is kinda self describing. It’s a software defined machine. The CPU, RAM, HDD… are all ‘virtualised’.

Hyper-Visor

A hyper-visor is a special type of software/operating-system that is built to facilitate the creation and hosting of virtual machines.

Type II HV

There are two types of Hyper-Visor, Type I & II. But for our use we only care about Type II. A Type II HV is a program that runs in a host OS like macOS or Windows. Examples of such HVs are, Oracle Virtual Box & UTM, the ones you’re using in this course!