# Bits and Bytes

Hi! There are many vocab terms I do not know how to incorporate into my codes even after looking the reference and tutorials section like unsigned long, unsigned char and a couple others. The one thing i get out of looking t the reference and tutorials over and over agin is the fact that they are very similar but are each used depending on the amount of bits or bytes, I don't know. My question is pretty much what are bits and bytes and how do they tie into programming with Arduino? THANK YOU

ETHAN :) :) :)

Bit is the smallest unit of information computers work with. It is a 1 or 0. https://en.wikipedia.org/wiki/Bit

Byte is a group of 8 bits. https://en.wikipedia.org/wiki/Byte

Different variable sizes take up different amount of memory and can hold numbers of different size. Here is the list of different data types you can use in Arduino environment and their limits. https://learn.sparkfun.com/tutorials/data-types-in-arduino

Thank you so much @Shpaget ! The definitions and I totally think Ill be checking out those links a ton. Already looked at the bottom one and it is perfect!!!

Ethan

:) :) :) :) :)

Random thoughts about bits & bytes...

- You can do a LOT of programming without worrying about bits & bytes. It's important to know that a signed integer can hold values between about +/- 32,000, (decimal) etc. And, if you don't remember the exact limits you can always look it up. Sometimes it's handy to know that a type int is 16-bits (etc.) but most of the time you only need to know the decimal size limits or any particular variable type (and if it's floating-point or integer and if its signed or unsigned).

- If you are programming an microcontroller or writing a driver where you are "close to the hardware", you often DO need to understand bits & bytes. But with the Arduino programming language, you can write a "bit" to an output pin (high or low, 1 or zero) or read a bit from an input pin without even thinking about it as a bit.

- The Windows Calculator in Programmer View can convert between decimal, binary, hexadecimal, and octal. (Octal is rarely used.)

- When we are using bits, we are usually thinking of a variable as a bit-pattern rather than a number. For example I've made some lighting effects with the Arduino, and the on/off state of one light is represented by one bit in a variable. I have a 4-channel effect where the 4 lights are represented by the 4 least significant bits in a byte. So if I have a binary value of 00000101, the 4 lights are OFF-ON-OFF-ON (the 4 most significant bits are just ignored). That variable has a decimal value of 5, but I don't care about that. I have another lighting effect where the state of 24 LEDs are determined by the 24 least significant bits in a type long (32-bits). One of my lighting effects is a chase-light effect and I just use bit-shifting without ever knowing or caring about the numerical value of the variable.

The concept of a "bit pattern" isn't much different from other non-numerical variables/values... The [u]ASCII[/u] character 'A' is stored in memory as the binary "number" 00100001 (decimal 65). If we add one to that variable we now have an upper case 'B'. Only be knowing the context... The purpose of that binary number in memory... can we know if it's supposed to represent an 'A', or the number 65, or a machine code instruction, or something else. If you open a file with a hex editor, you'll see 'A' everywhere there's a byte equal to the decimal value 65 even if that value doesn't represent an 'A' in the file. The hex editor doesn't know the context.

- When working with bits & bit patters, we often use hexadecimal in the program. That's because you can easily learn to convert variables of any size between binary and hex in your head. That's not so easy with decimal. The reason it's easy is because each group of 4 bits converts exactly to one hex digit. That means you only have to learn 16 conversions. You already know one and zero, and some patterns/numbers are just "easy to remember".

Here are some example variables to demonstrate how these bit patterns (binary numbers) are easily converted to hex values.

NOTE - It's sort-of "traditional" to write binary numbers in groups of 4 bits to make it easier for humans to read and convert (although, you can't do that if you use binary numbers in your program/sketch because the compiler won't understand it.) So although there are really no spaces in the number, I'll write it as 0101 0101 instead of 01010101, etc.

0101 0101 (binary) = 55 (hex) = 85 (decimal)

0001 0001 (binary) = 11 (hex) = 17 (decimal)

0001 0101 (binary = 15 (hex) = 21 (decimal)

1010 1010 (binary) = AA (hex) = 170 (decimal)

0111 0111 (binary) = 77 (hex) = 119 (decimal)

1111 1111 (binary) = FF (hex) = 255 (decimal)

0101 0101 0101 0101 (binary) = 5555 (hex) = 21,845 (decimal)

...I did the hex conversions in my head, but I used a calculator to get the decimal values, except I did already knew that a byte full of ones (the maximum value of a byte) is 255 (decimal) or FF (hex).

THANK YOU @DVDdoug !!! Everything was very help full and I already copy and pasted everything onto a doc in this "resources" folder I have. If you do respond, could you please explain the middle to the end a little more where you talk about decimals, 'A', and binary and hex. If not, I appreciate the thorough response! +1

To stir the pot a bit, "0101 0101 (binary) = 55 (hex) = 85 (decimal)" is two nibbles, or more accurately, two nybles. Two nybles make a byte. The old Burroughs medium system main frame was addressable to the nyble.

The names "byte" and "nyble" came from IBM in England, as I recall.

Paul

Paul_KD7HB: To stir the pot a bit, "0101 0101 (binary) = 55 (hex) = 85 (decimal)" is two nibbles, or more accurately, two nybles. Two nybles make a byte. The old Burroughs medium system main frame was addressable to the nyble.

The names "byte" and "nyble" came from IBM in England, as I recall.

Paul

Thank you @Paul_KD7HB!!! The quote above is very helpful. I now understand they are all equal but what are they used for and especially what does 0101 0101 (binary) mean? Just trying to understand what 85(decimal) for example means and why you would need it?

Thank you!!!, Ethan

They are showing you the relationship between different number systems. They will be on your first computer science test!

If you are interested in programming in assembly language for any computer, you will need to be able to convert from one number system to another. In addition, you will learn to multiply and divide numbers by shifting a number left to multiply by 2 and shift right to divide by 2. So, 0101 0101 times two is 10101010. When trying to make a program run as fast as possible we eliminate loops and write stringy code that may have the same set of instructions duplicated several times. Loops waste time. Multiply and divide are almost always complicated loops.

You may be working with a number input from a device that supplies a number “85”, and you need to decide if you are working with an odd or an even number. You can quickly determine this by looking at the “85” as a binary number “0101 0101” and see if the low order bit is a zero or a one. A zero means the number is even, a one means that number is an odd number.

Class over!

Paul

DVDdoug:
Random thoughts about bits & bytes…

- You can do a LOT of programming without worrying about bits & bytes. It’s important to know that a signed integer can hold values between about +/- 32,000, (decimal) etc. And, if you don’t remember the exact limits you can always look it up. Sometimes it’s handy to know that a type int is 16-bits (etc.) but most of the time you only need to know the decimal size limits or any particular variable type (and if it’s floating-point or integer and if its signed or unsigned).

Be very careful with such assumptions about bit width and value ranges.
The C language does not mandate the bit width of its native types like
int, long, etc…

So while an int is 16 bits on the AVR, it can be 32 bits on other processors like an ARM, and even 64 bits in other environments.

If you really care about the exact size in bits of the type, then it is best to avoid all the native C types and use the fixed width types created in the ANSI C99 spec.

i.e.

uint8_t
int8_t
int16_t
uint16_t
uint8_t
int32_t

etc…

And then for situations where you want speed more than exact size or don’t need an exact size, you can use types that allow the compiler to pick the actual type (and width) for you by specifying that you may need “at least” a certain amount of bits or that you want it to be fast.
This can help because it gives the compiler a bit more flexibility which can help it optimizing the code for what is desired (size or speed).

http://en.cppreference.com/w/c/types/integer

– bill

Paul_KD7HB: They are showing you the relationship between different number systems. They will be on your first computer science test!

If you are interested in programming in assembly language for any computer, you will need to be able to convert from one number system to another. In addition, you will learn to multiply and divide numbers by shifting a number left to multiply by 2 and shift right to divide by 2. So, 0101 0101 times two is 10101010. When trying to make a program run as fast as possible we eliminate loops and write stringy code that may have the same set of instructions duplicated several times. Loops waste time. Multiply and divide are almost always complicated loops.

You may be working with a number input from a device that supplies a number "85", and you need to decide if you are working with an odd or an even number. You can quickly determine this by looking at the "85" as a binary number "0101 0101" and see if the low order bit is a zero or a one. A zero means the number is even, a one means that number is an odd number.

Class over!

Paul

Yep. Class over! Thank you so much @Paul_KD7HB ! Yes, you are correct. I have not got to any science computer test for i am still in middle school but thanks for preparing me! Everything seems to finally be coming together now! The final thing is just 'What are the different number systems?" Thank you, Ethan

Thank you @bperrybap ! That is very cool!!! So, is that just more specific ways of defining the variables. You said C programming, but that also would apply to C++? For example, INT16_t is equivalent to using a 16-bit int, if i am saying that correctly?

THANK YOU, Ethan

EQOxx123: Thank you @bperrybap ! That is very cool!!! So, is that just more specific ways of defining the variables. You said C programming, but that also would apply to C++? For example, INT16_t is equivalent to using a 16-bit int, if i am saying that correctly?

C++ is built on C so all the C types are available in C++

Case (upper or lower) matters so INT16_t is incorrect. It is int16_t

but yes int16_t means you will get a 16 bit integer when using that type.

OK thx @bperrybap ! How do you know know the number of bits your integer is?

EQOxx123: OK thx @bperrybap ! How do you know know the number of bits your integer is?

It depends on the platform. You have to read the datasheet Or other documentation.

As an aside, some of the earliest computers used a 6 bit byte.

EQOxx123: The final thing is just 'What are the different number systems?"

Number systems, to me, means the "base" of the number set. Binary is base 2, ie. 0 and 1 are the only members. A trinary system or base is 3, so 0,1,2 are in the set. Many years ago, Sperry-Univac, use a trinary number system in one of their computers. Another popular system used by Digital Equipment computers was based on 7. In it, if you add 1 to 6, you get 10 as the result.

How high can you go in number systems or bases? Well, you use one every day that came from the Babylonians. It is base 60. Can you guess where you use it? Think of 60 seconds. Numbered from 00 to 59. Add one and go back to 00 and add 1 to the minutes. Keep going and get to 59 minutes. Add one and the minutes go to 00 and one is added to the hour column.

The one system that does not fit a real number system is the "Roman numerals". You just have to memorize it.

Good luck in school. I hope I have interested you a bit in the computer science field.

Paul

Time to invest in a Decimal, HEX, BINARY, and OCTAL calculator for you studies. .

explain the middle to the end a little more where you talk about decimals, 'A', and binary and hex. If not, I appreciate the thorough response! +1...

...for and especially what does 0101 0101 (binary) mean? Just trying to understand what 85(decimal) for example means and why you would need it?

Decimal numbers are "regular" numbers. It's base-10 which means you have 10 digits, 0-9.

So 85 decimal is just a regular everyday number.

Binary is base-2. You only have 0 & 1. As you probably know, EVERYTHING in the computer's memory is ones & zeros.

So when you count in binary it looks like this: 1 10 11 100 101 110 111

If you convert those numbers to regular decimal numbers you get the 1 through 7.

You'll notice that all of those numbers are "in order" as they would be if they were regular decimal numbers and 11 comes after 10, etc.

If you have a variable X = 7 in your program and you use [u]bitRead()[/u] to read the individual bits, you'll find the 3 right-most bits are 1 and the other bits are zeros.

...for and especially what does 0101 0101 (binary) mean?

It could represent 85. Inmost cases, we don't care that somewhere deep inside the computer your number 85 is represented by ones & zeros. But, each bit may represent the on/off state of a switch, or with my lighting effects the on/off state of a light. So one byte (8 bits) can represent the state of 8 switches or 8 lights, etc.

Or if you go back to the ASCII chart, that 01010101 pattern may represent the letter "U". And, in that case we don't care about the binary value or the decimal value. We just want to see a "U".

In hexadecimal, we have sixteen digits. 0-9 followed by A-F. The hex digit A is equal to the regular decimal value of 10 and hex F is equal to 15.

So when you count in hex it looks like this: 1 2 3 4 5 6 7 8 9 A B C D E F 10

Note that 10 in hex is NOT ten. It's read as "one zero hex" and it's equal to the decimal value 16.

12, as in a dozen eggs is written as C in hex, or as 1100 in binary.

And again, we usually use hex when we are thinking about bit patterns rather than numbers. A 16-bit integer that's a "bit pattern" of all ones can be written in hex as FFFF instead of 1111111111111111 or instead of 65,535. (65,535 would be an unsigned integer. With signed integers, the leftmost bit is the sign bit with zero representing positive and one representing negative numbers. Negative numbers are represented in a format called two's complement but we'll save that for another day...)

Thank you everyone! You all were extremely helpful and I admire you all a lot! So, pretty much 0101 0101(BIN) = 85(DEC) = FFFFFA((15(F) * 5)+ A(10) but knowing how FFFF = 65,535, that definitely can't be right. +1 all. Ya... another day. [b]THANK YOU[b]

0101 0101(BIN) = 85(DEC) = FFFFFA

Err no.
Just no.
Before you get onto negative number representations, just spend a little while pondering common number bases.
100 = 1
101 = 10
102 = 100
And so on.
So, in hex,
160 = 1, 0x01
161 = 16, 0x10
162 = 256, 0x100
163 = 4096, 0x1000
164 = 65536, 0x10000

So, 0x10000 is 65536 decimal, so one less is 0xffff or 65535 decimal

@Groove, I know about digital exponents like 100, 105, etc. (all digital exponents) but when it gets to Hexadecimal conversions it gets tricky for me. Yes, 164 equals 65,536 because that is 16x16x16x16 but how does that also equal 0x10000 which as I would have guessed would be 0. THX,
Ethan