Go Down

Topic: uint8_t ? (Read 1 time) previous topic - next topic

Lightfoot77

I keep running into this type of int command in various sketches but have been unable to find out what it does exactly.  I have googled it and even put it in the Arduino Search above which only kicks me back out to google.  Sorry for such a silly request but I enjoy the coding a bit more then the Hard :o  :o ware and like to understand it.  Thanks :)

J-M-L

#1
Oct 21, 2016, 09:07 am Last Edit: Oct 21, 2016, 09:18 am by J-M-L
It's a type of variable like char or int or long.

In that case it represents a type (hence the _t at the end) of an unsigned (hence the u at the begining) integer (hence the int) number stored in memory on one byte in arduino's memory (really 8 bits - hence the 8 - and most computers have 8 bits for a byte)

So const uint8_t motorDirectionPin = 3; declares a constant, requiring 8 bits of memory, representing an unsigned number and initialized with the value 3 that you can refer too later in the code with the name motorDirectionPin.

You'll find as well

int8_t    uint8_t   
int16_t   uint16_t
int32_t   uint32_t
int64_t   uint64_t

Integer type with a width of exactly 8, 16, 32, or 64 bits.
For signed types (no u in front of the type), negative values are represented using 2's complement.
No padding bits.


You need to brush up your googleFoo because the first 3 search answers  for "uint8_t C++" give me the correct info

See this for more info


Hope this helps!

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Budvar10

#2
Oct 21, 2016, 09:11 am Last Edit: Oct 21, 2016, 09:17 am by Budvar10
The uint8_t is typedef of classic C 'unsigned char' aka byte. Similary the other types. They are defined in stdint.h (AVR 8 ). I think, just read this header file and everything will be clear.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Lightfoot77

So basically anything C++ goes with the Ide.  Cool!  Makes sense as its based on the G++ compiler.  It can not be a wasted day when I have learned something new.  Thank you for that fantastic breakdown!

J-M-L

Yes there are a few functions not implemented by default because they would bloat the binary code to unreasonable size (like handling float in some string functions) but you are pretty safe with the c++ standard as your guide.
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Lightfoot77

So const uint8_t motorDirectionPin = 3;  declares a constant, requiring 8 bits of memory, representing an unsigned number and initialized with the value 3 that you can refer too later in the code with the name motorDirectionPin.





So what is the advantage over say
int motorDirectionPin=3;

? Sorry Im think headed :)

J-M-L

#6
Oct 21, 2016, 09:31 am Last Edit: Oct 21, 2016, 09:36 am by J-M-L
It helps the compiler. If that name is declared as a const, so it won't change, the compiler is likely not going to allocate memory for it at all and will just substitue  in assembly language the reference to the memory fetch you would need to do with the number 3. So less memory used and faster code.

It will also bark at you if you have a bug and later in your code  you try to change the value to something else by mistake because const can't change value. So that is a benefit as well in terms of code quality.

Your version will  (compilers are smart though so might notice you use it as a const) allocate two bytes instead of one that is enough for storing the value 3, will trigger then two bytes fetch in memory every time you use it, and most likely then use only the low byte because functions you use it with will require a byte, not two...

Basically the more info about your intent you can give to the compiler, the better it will optimize your code and help you with not creating bugs.
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Lightfoot77

Thank you, that was crystal clear, and thanks for the c++ reference as well, good stuff !

J-M-L

With pleasure.

Have fun with your code!
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Lightfoot77

Good stuff, trials show that Im saving about 1 byte per variable using uint8_t over the int.  Again its never a wasted day if i have learned something new ...

Go Up