Signed and Unsigned Integers

What does that even mean! 😭 — A Golang primer

Nikasulo
Geek Culture
5 min readAug 4, 2021

--

A Signed variable in programming refers to a variable that can hold both positive and negative values and unsigned refer to variables that can only hold positive values.

In a language like Golang, you can specify which types of integers you want a variable to be able to hold, this is particularly important and helpful when you start to worry about memory optimization.

If you understand what a bit is and what a byte is, you’ll understand this next part, otherwise, check out my article on the subject of bits and bytes. A bit can represent anything if you want to dig into it but for the purpose of sanity we can say a bit can have 2 values, a 1 and a 0

This means that for every bit, you can generate 2 possible numbers. This would go on to mean that for 2 bits you can generate 4 possible numbers, and with 8 bits you can generate 255 numbers, and so on. Having this knowledge, it won’t be unusual to say that an 8bit number can go from 0–255.

Now Golang offers us a few integer types:

uint, int, uint8, int8, uint16, int16and so on up until the 64-bit variants of both integer types(signed and unsigned), in addition to the float and complex variants(which we won’t be talking about today 😉)

The “u” in front of each variant means unsigned, meaning that they can only hold positive values and the variants without the “u” are signed, meaning that can hold both negative and positive values.

Let’s go! 🚀

Taking our understanding a bit further the numbers on the end of each variant depict how many bits of data each of them can hold. If you recall, or remember from this article, 8 bits can hold 256 values, this would mean that an int8 and a uint8 variable can hold 256 values respectively but the range of possibilities are different.

Integer types in Golang

An unigned 8-bit integer,uint8, can hold values 0-255 , a total of 256 values, but what can we store in an 8-bit signed variant int8 ? Well we split 256 down the middle across both polarities, so 127 in the negative direction and 127 in the positive direction and then of course, in the center, we have a 0 👇🏽

By this same logic, we can say that a uint16 can hold 2¹⁶ possible values, which means 0-65535 .

At this point, you can probably guess that an int16 data-type can hold 32768through positive 32767 😉 and so on.

Why so many data types for integers?

Now the fun part is knowing when to use each data type. If you haven’t already read my article here or you don’t understand how the RAM works/how data is stored and accessed from the RAM, you may want to do some research. You may be wondering, why don’t we just use the largest possible variant all the time? An int64 can probably do it all no? 💪

Well, yes, but then there is the problem of limited memory. If you understand how data get stored in the RAM, you may understand that all the programs on your machine run in shared memory space, meaning that they all use up some portion of the RAM, you can take a gander at this article if you’re curious. Knowing this, you may also understand that your machines and programs can start to slow down when there is not enough free memory for those super-fast, complex operations and may be thinking of optimizing memory usage, understanding the concepts of the differently sized integer constructs can be of help to you, here is why.

Imagine you allocated a variable var a int , this variable will immediately block out 32-bits or 64-bits of RAM space, whether or not you actually store a number large enough to take up that space, this is wasteful, that is 32-bits of space that other programs on your machine will no longer have access to until your program releases that memory by garbage collection or some other fancy operation.

Now, this doesn’t mean you should go around prematurely optimizing memory allocations by sizing your integer variables, but if you absolutely need it and you are absolutely sure exactly how much space or what kinds of numbers are going to be stored in your variables, go for it! Your machine will love you ️❤️

This may also be important to you if you design systems that handle very large datasets and store a lot of data, imagine storing 10million records that could’ve been uint8 integer types in uint64 space 😭 that’s 560MB of space WASTED.

All of this? At a very high level is why this matters. Sometimes memory is the name of the game and you want to play it close to the chest. Again, this is great knowledge to have, but the difference between a good and a great Engineer is knowing what and when to optimize.

Thank you for following along! Until next time 👋

--

--

Nikasulo
Geek Culture

I build for the web. I teach people to build for the web. You can follow me closely here https://twitter.com/nervousrubyist, ask me questions about code 😉