Hi, I have a question. Can someone explain why we use byte data type?
For exemple:
const byte ROWS = 4;
const byte COLS = 4;
Hi, I have a question. Can someone explain why we use byte data type?
For exemple:
const byte ROWS = 4;
const byte COLS = 4;
Because there isn't a nibble data type.
To save space or to improve performance. Performance would only be improved on 8-bit Arduino like Uno, Mega. On 32-bit Arduino like Due, Zero, using byte will probably not give any performance improvement.
Variables of type byte can hold values 0 to 255 only. No negative values or values over 255 can be stored. Then you must use int or some other type.
Because there isn't a nibble data type.
. ... or a nybble
A byte saves 50% of the memory that would otherwise be assigned to a 16-bit integer if you working within the range of 8-bits (0-FF)
"byte" is a type alias for "uint8_t", which would have been a better choice in this case, because it tells you it's an unsigned integer that's 8 bits wide.
As a rule of thumb, use "byte" when dealing with binary data that doesn't necessarily represent an integer, and "uint8_t" for numbers between 0 and 255.
This is just a convention, of course, they're the same type on Arduino.
Pieter
...but be careful if you're used to Java, where "byte" is signed.
a JAVA byte is SIGNED !!
What numpty decided that ?
You mean "what numpty decided an Arduino byte isn't signed?"
I think a byte being unsigned makes much more sense.
I would expect byte(0xF0) >> 4
to be 0x0F
, not 0xFF
.
A byte type is not intended as ‘numeric’ storage... its a data type for memory allocation of 8 bits.
8 bits have no place to be signed or otherwise, they are the (typically) smallest native storage element in a system. (nybbles / bools/ bits are a managed subset of defined data types)
A byte can conveniently be used for storing an 8-bit numeric variable, but it’s lower down the tree of data types.
Larger numeric types e.g. longs, floats etc are multi-byte data types, each individual byte is not signed or unsigned, they simply exist. The numeric ‘type’ determines whether the MSb is a sign or not.
Hi,
I understand that nybble / nibble may be used as a special case here, but my understanding from learning processors from first principles so many decades ago was that a nibble represents four bits or 'half a byte'.
Is the term used in a different way on Arduino please?
Peter
Peterd51:
Hi,I understand that nybble / nibble may be used as a special case here, but my understanding from learning processors from first principles so many decades ago was that a nibble represents four bits or 'half a byte'.
Is the term used in a different way on Arduino please?
Peter
I remember the Intel 4004, and I don't understand your question.
nybble = 4-bits
CORRECT.
The is in deference to it’s ‘parent’ the bYte
Like you, I learned this back when 2K was a lot of RAM !
2k?
You're talking bits, right?
Nup, 2K Bytes...
My first evaluation board Signetics 2650 had 512B, which I spent up big to add 8K - which I never filled (!)
The on to Z80s and CP/M with 64KB !!!
lastchancename:
Nup, 2K Bytes...
Noob.
PieterP:
"byte" is a type alias for "uint8_t", which would have been a better choice in this case, because it tells you it's an unsigned integer that's 8 bits wide.
Pieter
What does the _t mean?
As an alias, it’s imperfect, because byte is ambiguous (not necessarily an int or numeric value)
For clarity, us old timers would think of a byte as a collection of bits - like a word, or long word.
Potentially to hold a bit mask or other ‘non-integral’ value.
They could be referenced with other names which imply a numeric intent.
Yes, they can be expressed as numeric values, but the contents are unlikely to be processed as a number.
The " t " is for "type".
why do we use bytes?
this is a general computer science question. since the beginning of time all computers and chips think only in zeros and ones because of the nature of semiconductors in any type of calculation. one on/off is called a bit.
obviously a number from zero to one is kind of useless. but computers group bits together. this way through binary math we can use number. these groups of bits are called bytes. during early development the it was debated how many bits to use in a byte but today about every chip or data protocol groups 8 bits to form a byte.
so todays 8 bit byte can represent 256 possabilitys... or a number from 0 to 255.
in the background ....every other variable type we use in programming is just one or more bytes and can be converted back and forth.
a byte or byte array is the raw representation of any variable or data.
The question i have for everyone else is what happens when they figure out quantum computers?
no more bits and bytes? Guess we will all be learning absolutely everything all over again.