[{"content":"Overview Go provides four distinct sizes of signed and unsigned integers of size 8, 16, 32, and 64.\n   sign size     unsigned uint8, uint16, uint32, uint64   signed int8, int16, int32, int64    for a variable like uint8 and int8 compiler allocates an 8-bit address register and a 32-bit register for uint32 and int32.\nTo see integer overflow in action we\u0026rsquo;ll take two examples of both int8 and uint8 to cover signed and unsigned integers.\nuint8 1 2  var u uint8 = 255 fmt.Println(u, u+1, u*u) // \u0026#34;255 0 1\u0026#34;   we add one to variable u then it becomes 256 (100000000 in base 2), as the register is only 8-bit long. Still, we have 9-bit to store so, in this case, the most significant (also known as left-most or high-order) bits are discarded except the last 8-bit and those last 8-bit are stored in the variable u. last 8-bit of 100000000 are 00000000 which is 0 (base 10).\nnext we store 255*255= 65025 which is 1111111000000001 (base 2). So after discarding the high-order bits we left with 00000001 which is 1 (base 10).\nint8 Now take another example for a signed integer.\n1 2  var i int8 = 127 fmt.Println(i, i+1, i*i) // \u0026#34;127 -128 1\u0026#34;   For a signed integer the most significant (left-most) bit indicates the sign of the integer, which is 1 for negative and 0 for positive.\nSo 127 is 01111111 (base 2) which starts with 0. So when we add 1 to it. it becomes 10000000 (base 2). if it was a type of uint8 then the value would be 128. but it is a signed number so the number is negative. 10000000 (base 2) represents -128 in two\u0026rsquo;s complement representation of the negative number.\nagain we calculate 127 * 127 = 16129 which is 11111100000001 (base 2) and store it in the i then only the last 8-bit will be kept which is 00000001 (base 2) i.e. 1.\nbonus how 10000000 (base 2) is -128? According to Two\u0026rsquo;s complement method we invert all the bits and add one to it.\n   -128 : 10000000     invert bits: 01111111   add one: 10000000    Final bits we get is 10000000, which is 128.\n","permalink":"https://f1rstmehul.github.io/posts/integer_overflow/","summary":"this post shows how integer overflow happens","title":"Integer overflow (in Go)"},{"content":"I\u0026rsquo;m a software engineer \u0026amp; amateur astronomer. On the internet, I’m f1rstmehul.\nI enjoy writing code, especially in Go and Rust.\nDistributed systems and compilers interest me the most. When I\u0026rsquo;m not close to a monitor, I’m usually looking up at the sky or out on a cricket field.\nI\u0026rsquo;m on, x (or twitter), github and linkedin.\n","permalink":"https://f1rstmehul.github.io/about/","summary":"I\u0026rsquo;m a software engineer \u0026amp; amateur astronomer. On the internet, I’m f1rstmehul.\nI enjoy writing code, especially in Go and Rust.\nDistributed systems and compilers interest me the most. When I\u0026rsquo;m not close to a monitor, I’m usually looking up at the sky or out on a cricket field.\nI\u0026rsquo;m on, x (or twitter), github and linkedin.","title":"About Me"},{"content":"Listing soon\u0026hellip;\n","permalink":"https://f1rstmehul.github.io/projects/","summary":"Listing soon\u0026hellip;","title":"Projects"}]