{"base":{"id":"cs10-04","subject":"Data representation","title":"Binary addition, overflow and shifts","scene":"bin10","steps":[{"mode":"addrules","eyebrow":"Four rules","title":"Adding in binary","body":"Binary addition works like column addition, but you carry at 2 instead of 10. There are only four facts to learn. Tap each one.","actions":[{"id":"r0","label":"0 + 0"},{"id":"r1","label":"0 + 1"},{"id":"r2","label":"1 + 1"},{"id":"r3","label":"1 + 1 + 1"}]},{"mode":"add","eyebrow":"Column addition","title":"Add two bytes","body":"Work from right to left. Write the result bit underneath and the carry above the next column. Change the two numbers and check the answer by converting.","controls":[{"id":"a","label":"First number","min":0,"max":255,"step":1,"value":90},{"id":"b","label":"Second number","min":0,"max":255,"step":1,"value":53}]},{"mode":"add","eyebrow":"Overflow","title":"When 8 bits are not enough","body":"A register holds a fixed number of bits. If the answer needs a 9th bit, it is lost: an overflow error. 200 + 100 = 300, but 300 doesn't fit in a byte. Find where overflow starts.","controls":[{"id":"a","label":"First number","min":0,"max":255,"step":1,"value":200},{"id":"b","label":"Second number","min":0,"max":255,"step":1,"value":100}]},{"mode":"shift","eyebrow":"Logical shifts","title":"Slide the bits","body":"A logical shift moves every bit left or right and fills the gap with 0. Each left shift doubles the number; each right shift halves it. Shift too far and bits fall off the end.","controls":[{"id":"n","label":"Number","min":0,"max":255,"step":1,"value":13}],"actions":[{"id":"left","label":"Shift left"},{"id":"right","label":"Shift right"},{"id":"reset","label":"Reset"}]},{"mode":"twos","eyebrow":"Negative numbers","title":"Two's complement","body":"To store negatives, make the leftmost bit worth −128 instead of +128. A byte then holds −128 to 127. To make −n: write n, flip every bit, add 1.","controls":[{"id":"n","label":"Number","min":-128,"max":127,"step":1,"value":-45}]},{"mode":"quiz","look":"addrules","eyebrow":"Check yourself","title":"Shift left twice","body":"The byte 00110101 (53) is shifted 2 places to the left.","question":"What is the result?","options":["11010100","00001101","01101010"],"answer":0,"right":"Correct. Every bit moves 2 places left and two 0s fill the right: 11010100 = 212 = 53 × 4.","wrong":"Not quite. Left shift: move every bit left, fill with 0s. Two shifts multiply by 4."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","intro":"Write carries small, above the next column to the left.","items":[{"tag":"Addition","title":"01011011 + 00110110","body":"carry   111111\n         01011011   (91)\n       + 00110110   (54)\n       ----------\n         10010001   (145)\ncheck: 91 + 54 = 145 ✓","code":true},{"tag":"Overflow","title":"11001000 + 01100100","body":"         11001000   (200)\n       + 01100100   (100)\n       ----------\n       1 00101100\n         ↑ 9th bit lost\nstored: 00101100 = 44, not 300","code":true},{"tag":"Shift right","title":"10110100 shifted right 3 places","body":"10110100 (180)\n→ 00010110 (22)\n180 ÷ 8 = 22.5 → 22\nthe 1 that fell off made it round down","code":true},{"tag":"Two's complement","title":"Write −20 as an 8-bit number","body":"+20 = 00010100\nflip  11101011\nadd 1 11101100\ncheck: −128 + 64 + 32 + 8 + 4 = −20 ✓","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Writing 2 in a binary answer","body":"There is no digit 2 in binary. 1 + 1 = 10: write 0, carry 1."},{"tag":"Mistake","title":"Forgetting the carry in the next column","body":"1 + 1 with a carry of 1 is 11: write 1 and carry 1 again."},{"tag":"Mistake","title":"\"Overflow means the answer is too big for the screen\"","body":"Overflow means the result needs more bits than the register has. The extra bit is lost and the stored value is wrong."},{"tag":"Mistake","title":"Flipping bits but not adding 1","body":"Flipping alone gives one's complement. Two's complement needs the + 1 as well."}]},{"type":"cards","id":"method","eyebrow":"Set out your answer","title":"Getting full marks","items":[{"tag":"1 · Line up the bits","title":"Same width, right-aligned","body":"Write both numbers as full bytes under each other, with carries above."},{"tag":"2 · Say what overflow does","title":"Name the effect","body":"\"The result needs 9 bits but only 8 are available, so the most significant bit is lost and the result is incorrect.\""},{"tag":"3 · Describe a shift's effect","title":"Multiply, divide and lose","body":"\"A left shift of 2 multiplies by 4. If 1s are shifted off the left, the result is wrong. A right shift loses the bits on the right, losing precision.\""}]},{"type":"bars","id":"ranges","eyebrow":"Data","title":"What can a byte hold?","intro":"The same 8 bits mean different things depending on how they are read.","items":[{"label":"Unsigned","value":100,"display":"0 to 255","color":"#2f6fdb"},{"label":"Two's complement","value":100,"display":"−128 to 127","color":"#e0463e"},{"label":"16-bit two's complement","value":100,"display":"−32 768 to 32 767","color":"#7c3aed"}],"facts":[{"title":"11111111 is two different numbers","body":"Read unsigned, it's 255. Read as two's complement, it's −1. The program decides which."},{"title":"Why two's complement wins","body":"Ordinary binary addition just works: 00000101 (5) + 11111011 (−5) = 1 00000000, and dropping the carry leaves 0. No extra circuits are needed for subtraction."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Real world","title":"The 'Gangnam Style' counter","body":"In 2014 YouTube's view counter used a 32-bit signed number, maximum 2 147 483 647. One video got close to that, so engineers switched to 64-bit counters before it could overflow."},{"tag":"History","title":"Ariane 5, 1996","body":"Forty seconds after launch, the Ariane 5 rocket broke up. Software converted a 64-bit value into a 16-bit integer; the number was too big and overflowed. A loss of hundreds of millions of dollars, from one line of code."},{"tag":"How it works","title":"Shifts are super fast","body":"Multiplying by 2, 4 or 8 with a shift takes the processor a single tiny step, much quicker than general multiplication. Compilers swap multiplications for shifts automatically."},{"tag":"Coming next","title":"Where this leads","body":"Next: how text, pictures and sound are all stored as these same binary numbers."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one before opening the card.","items":[{"tag":"Challenge","title":"Use two's complement to work out 35 − 12","body":"35 = 00100011\n−12: 12 = 00001100 → flip 11110011 → +1 11110100\nadd: 00100011 + 11110100 = 1 00010111\ndrop the 9th bit: 00010111 = 23 ✓","code":true},{"tag":"Challenge","title":"How can you multiply by 10 using only shifts and one addition?","body":"10x = 8x + 2x.\nShift left 3 (× 8), shift the original left 1 (× 2), then add the two results.\nExample: 6 → 48 + 12 = 60."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Binary maths","items":[{"title":"Binary addition race","time":"15 minutes","need":["A friend","Paper","A timer"],"steps":["Each write two random bytes.","Swap and add them in binary.","Check by converting all three numbers to denary.","Who can do five correctly the fastest? Did anyone overflow?"],"why":"Checking with denary proves the binary rules work, and shows when an answer needs a 9th bit.","safety":"No safety risks."},{"title":"Shifts in Python","time":"15 minutes","need":["Python or an online editor"],"steps":["Type print(13 << 1), print(13 << 3) and print(13 >> 2).","Predict each answer before running it.","Try print(bin(13 << 3)) to see the bits.","Find a shift that turns 7 into 56."],"why":"<< and >> are Python's shift operators: << n multiplies by 2ⁿ, >> n divides by 2ⁿ and drops the remainder.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"What is 1 + 1 in binary?","options":["10","2","11"],"answer":0,"right":"Correct. Write 0, carry 1.","wrong":"Not quite. Binary has only the digits 0 and 1."},{"question":"What is 0101 + 0011?","options":["0110","1000","1111"],"answer":1,"right":"Correct. 5 + 3 = 8 = 1000.","wrong":"Not quite. Add column by column, carrying at 2."},{"question":"When does overflow happen in 8-bit addition?","options":["When the answer is negative","When the answer is odd","When the answer is bigger than 255"],"answer":2,"right":"Correct. 255 is the biggest number 8 bits can hold.","wrong":"Not quite. Think about the biggest number a byte can store."},{"question":"A number is shifted left 3 places. What happens to its value?","options":["× 8","× 3","+ 3"],"answer":0,"right":"Correct. Each left shift doubles: 2 × 2 × 2 = 8.","wrong":"Not quite. One left shift doubles the value."},{"question":"What is 01100000 shifted right 2 places?","options":["10000000","00011000","00110000"],"answer":1,"right":"Correct. 96 ÷ 4 = 24 = 00011000.","wrong":"Not quite. Move every bit 2 places right and fill with 0s."},{"question":"In 8-bit two's complement, what is the leftmost bit worth?","options":["128","1","−128"],"answer":2,"right":"Correct. That's how negative numbers are made.","wrong":"Not quite. The leftmost bit has a negative place value."},{"question":"What is 11111111 in 8-bit two's complement?","options":["−1","255","−127"],"answer":0,"right":"Correct. −128 + 127 = −1.","wrong":"Not quite. Use −128 for the leftmost bit."},{"question":"Shifting 11000000 left by 1 in an 8-bit register gives…","options":["384","10000000 (wrong answer: a 1 is lost)","01100000"],"answer":1,"right":"Correct. The leftmost 1 falls off, so the result is 128, not 384.","wrong":"Not quite. What happens to the bit that moves out of the register?"}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["state the four rules of binary addition","add two 8-bit binary numbers with carries","check binary addition by converting to denary","explain what overflow is and why it causes errors","perform logical left and right shifts","describe the effect of a shift on the value","convert negative numbers to and from two's complement","give the range of an 8-bit unsigned and signed number"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"carry","meaning":"A 1 moved to the next column when a column adds to 2 or more."},{"word":"register","meaning":"A small, fast store in the processor with a fixed number of bits."},{"word":"overflow","meaning":"When a result needs more bits than the register has."},{"word":"logical shift","meaning":"Moving all bits left or right, filling gaps with 0."},{"word":"most significant bit (MSB)","meaning":"The leftmost bit, with the biggest place value."},{"word":"least significant bit (LSB)","meaning":"The rightmost bit, worth 1."},{"word":"unsigned","meaning":"A number that can only be zero or positive."},{"word":"two's complement","meaning":"A way to store negative numbers: the MSB is worth −128 in a byte."},{"word":"precision","meaning":"How exact a value is; lost when bits fall off the right."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize, GCSE Computer Science: Binary addition and shifts","ESA: Ariane 501 Inquiry Board report (1996)","Isaac Computer Science: Two's complement","Python documentation: bitwise operators"]}]},"overlay":null}