### Just Multiplication

If you're worried about overflow or underflow, do your math inside a `checked` block. It'll throw an OverflowException if the result wouldn't fit.

Code
Diff
• ``````using System;

public class Multiplication
{
// Multiply two integers
// Throws error if the product is outside of the int range.
// This would otherwise silently error and return the wrong product.
public long Multiply(int a, int b) {
checked { return a * b; }
}
}``````
• using System;
• public class Multiplication
• {
• // Multiply two integers
• // Throws error if the product is outside of the int range.
• // This would otherwise silently error and return the wrong product.
• public long Multiply(int a, int b) {
• long res = (long)a * (long)b;
• // make sure it's still a valid int
• if (res > Int32.MaxValue || res < Int32.MinValue)
• throw new InvalidOperationException("Product is outside range of int");
• return res;
• checked { return a * b; }
• }
• }

### Cheating through internal stack?

Return addresses are just normal stack entries, and nasm lets you jump to an address from a register.

Code
Diff
• ``````global stack_push, stack_pop, stack_peek ; , stack_is_empty
section .text
stack_push:
xchg rsi, [rsp]
jmp rsi
stack_pop:
pop rsi
pop rax
jmp rsi
stack_peek:
pop rsi
mov rax, [rsp]
jmp rsi
``````
• global stack_push, stack_pop, stack_peek ; , stack_is_empty
• section .text
• stack_push:
• push rsi
• ret
• xchg rsi, [rsp]
• jmp rsi
• stack_pop:
• pop rsi
• ret
• pop rax
• jmp rsi
• stack_peek:
• pop rsi
• push rsi
• ret
• mov rax, [rsp]
• jmp rsi

### and, or and xor without "&&" and "||"

• get rid of comparisons; just let boolean conversion take care of it
• and is better represented as `*`
Code
Diff
• ``````bool Or(bool a, bool b){
return a + b;
}

bool Xor(bool a, bool b){
return a != b;
}

bool And(bool a, bool b){
return a * b;
}
``````
• bool Or(bool a, bool b){
• return a + b > 0;
• return a + b;
• }
• bool Xor(bool a, bool b){
• return a != b;
• }
• bool And(bool a, bool b){
• return a + b == 2;
• return a * b;
• }

### Game of Life (with tests):

Got rid of most of the ifs

Code
Diff
• ``````function nextGeneration(grid) {
return grid.map((row, rowIndex) => {
return row.map((cell, colIndex) => {
if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
let neighboursCount = grid[rowIndex][colIndex + 1] +
grid[rowIndex][colIndex - 1] +
grid[rowIndex + 1][colIndex + 1] +
grid[rowIndex + 1][colIndex] +
grid[rowIndex + 1][colIndex - 1] +
grid[rowIndex - 1][colIndex + 1] +
grid[rowIndex - 1][colIndex] +
grid[rowIndex - 1][colIndex - 1];
return 0 + (neighboursCount === 3 || (cell === 1 && neighboursCount === 2));
}
return 0;
});
});
}``````
• function nextGeneration(grid) {
• return grid.map((row, rowIndex) => {
• return row.map((cell, colIndex) => {
• if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
• let neighboursCount = (grid[rowIndex][colIndex + 1] === 1) + (grid[rowIndex][colIndex - 1] === 1) + (grid[rowIndex + 1][colIndex] === 1) + (grid[rowIndex - 1][colIndex] === 1) + (grid[rowIndex + 1][colIndex + 1] === 1) + (grid[rowIndex + 1][colIndex - 1] === 1) + (grid[rowIndex - 1][colIndex + 1] === 1) + (grid[rowIndex - 1][colIndex - 1] === 1);
• if (cell === 1) {
• if (neighboursCount === 2 || neighboursCount === 3 ) {
• return 1;
• }
• } else {
• if (neighboursCount === 3 ) {
• return 1;
• }
• }
• return 0;
• let neighboursCount = grid[rowIndex][colIndex + 1] +
• grid[rowIndex][colIndex - 1] +
• grid[rowIndex + 1][colIndex + 1] +
• grid[rowIndex + 1][colIndex] +
• grid[rowIndex + 1][colIndex - 1] +
• grid[rowIndex - 1][colIndex + 1] +
• grid[rowIndex - 1][colIndex] +
• grid[rowIndex - 1][colIndex - 1];
• return 0 + (neighboursCount === 3 || (cell === 1 && neighboursCount === 2));
• }
• return 0;
• });
• });
• }