# Binary logic

#### ZimmerJ

Joined Dec 9, 2020
58
Hello,
To start of i want to say that i have basic knowledge in terms of electric/magnetic phenomena. I'm am now learning more about electronics.

I am struggling to understand how the binary numbers can be different bit-values. I'm at this moment programming with the arduino board in my class, and the simple project i am working on is to make a led-light dim, in other words make it have a fade effect. It's quite easy, but to explain my "problem":

Brightness is the integer for the light emitted, and has value between 0-255. The fade-amount integer is set to jump in steps of 5. So as the fade goes up or down, it happens with 5´s between 0-255. The range of 0-255 is what i want to understand. I know the arduino board is an 8-bit microcontroller, and a byte can represent a decimal value of maximum 255, hence the range. Then looking into it, it would mean that different 1's will have different values in them selves, at least when you look at the binary table.

For example the 1st binary 1, would represent decimal value 1, while the 8th binary 1 would represent decimal value 128. So dependent on what value the microcontroller is suppose to send on the output, it will choose between which 1's will be "on"?

Lets say i want 128/255 on the output, the binary would be set to: 10000000.
But if i want 1/255 on the output, the binary would be set to: 00000001.

So the are still just one binary 1 in each code, but obviously they are not by the same value. In this case, what determines these values for the different binary numbers? Yes i want to know the reality of these electrical signals, and how they can vary. My first thought is that they represent different voltage levels, if so, it is then determined by the electric connections in an IC?

I have also read a little about logic gates, registers etc. But not too much.

Joined Jul 18, 2013
26,347
You seem to be jumping around the bases.
You usually work in one or the other whether base 2 (binary) , base 8 (octal), base 10 (decimal) Base 16 ( Hexadecimal) etc.
Convert a result to one or the other any time.
Max.

#### ZimmerJ

Joined Dec 9, 2020
58
You seem to be jumping around the bases.
You usually work in one or the other whether base 2 (binary) , base 8 (octal), base 10 (decimal) Base 16 ( Hexadecimal) etc.
Convert a result to one or the other any time.
Max.
Well, yes. As the fade-effect is to be read as an analog value, decimal is chosen by arduino to display it, out of simplicity i guess. But the decimal numbers are based on the binary numbers, dependent on with 1's that are sent. So i am wondering how the electric signals (binary 1's and 0's), can vary. It seems to be a mixture of voltage levels and the technology structure of the IC.

#### jpanhalt

Joined Jan 18, 2008
11,087
Lets say i want 128/255 on the output, the binary would be set to: 10000000.
But if i want 1/255 on the output, the binary would be set to: 00000001.

So the are still just one binary 1 in each code, but obviously they are not by the same value. In this case, what determines these values for the different binary numbers? Yes i want to know the reality of these electrical signals, and how they can vary. My first thought is that they represent different voltage levels, if so, it is then determined by the electric connections in an IC?
In each case a "1" is a 1. But the decimal value of each place is 128 or 1, respectively.

What if those were decimal numbers?
10000000 would be written as 10,000,000, so the "1" would be 10 million = 1X10^7. In the second case, the 1 is still one.

Binary is a different base, powers of 2, instead of powers of 10. So, 1000 0000 = 1x2^7

#### bogosort

Joined Sep 24, 2011
696
So the are still just one binary 1 in each code, but obviously they are not by the same value. In this case, what determines these values for the different binary numbers?
Position. Base-2 number representation ("binary") is positional, just like base-10 ("decimal"). That is, the position of each bit/digit determines its value. It's the same concept as how the digit '9' in the base-10 number 900 has a different value than the digit '9' in the base-10 number 90.

If we index the first digit's position as zero, then the kth digit has a value of 10^k in base-10 and 2^k in base-2. To arrive at the final value, we multiply each digit by its positional value and sum, e.g., 101 in base-2 is 1*2^2 + 0*2^1 + 1*2^0, which is 5 in base-10.

Yes i want to know the reality of these electrical signals, and how they can vary. My first thought is that they represent different voltage levels, if so, it is then determined by the electric connections in an IC?
A binary system has, by definition, only two different levels (high and low). How these levels are physically organized is entirely arbitrary and makes no difference -- they can be "side by side" or treated as columns in a matrix or anything whatsoever. All that is required is that the system that writes the values and the system that reads the values agree on the organization.

#### ZimmerJ

Joined Dec 9, 2020
58
In each case a "1" is a 1. But the decimal value of each place is 128 or 1, respectively.

What if those were decimal numbers?
10000000 would be written as 10,000,000, so the "1" would be 10 million = 1X10^7. In the second case, the 1 is still one.

Binary is a different base, powers of 2, instead of powers of 10. So, 1000 0000 = 1x2^7
Yes, i understand how the different bases works in theory. But how then does it work in the microchip? Is it arithmetic? Something must be affecting the electric signals to be different values. dependent on which logic gates that are turned on?

#### MrChips

Joined Oct 2, 2009
27,743
Let's see if I can confuse you even further.

Think bits for now.
A single bit can have two states 0 and 1.
Two bits can have four unique states, 00, 01, 10, 11
:
:
eight bits can have 256 unique states.

Each state means nothing, or something, or whatever you want it to be.

Let's take two bits to keep it simple.
We can use the four unique states to represent four different attributes such as the colours, black, brown, gray, blue.
We can use it to tag four persons, John, Mary, Sue, Albert.
We can use it to represent four numeric values 123, 9876, 3.14, 0.002.

So it does not matter what you use 256 unique codes to represent. It is entirely up to the user to define what 11001010 represents.

• ZimmerJ

#### bogosort

Joined Sep 24, 2011
696
Yes, i understand how the different bases works in theory. But how then does it work in the microchip? Is it arithmetic? Something must be affecting the electric signals to be different values. dependent on which logic gates that are turned on?
Microchips are physical machines and do not know anything about numbers or base-2 or base-10. All a microchip can do is switch voltage levels between high and low. It turns out, however, that in the right context this simple capability is enough perform any computation whatsoever. It's up to humans to interpret the results of those computations. Likewise, it's up to humans to design useful ways to switch voltage levels. Your LED dimmer is likely using pulse-width modulation (fast, variable switching) to control the amount of power the LED receives. Someone has mapped the dimmer's PWM's duty cycle (which we interpret as 0 to 100%) to an 8-bit value (which we interpret as 0 to 255).

• ZimmerJ

#### ZimmerJ

Joined Dec 9, 2020
58
Microchips are physical machines and do not know anything about numbers or base-2 or base-10. All a microchip can do is switch voltage levels between high and low. It turns out, however, that in the right context this simple capability is enough perform any computation whatsoever. It's up to humans to interpret the results of those computations. Likewise, it's up to humans to design useful ways to switch voltage levels. Your LED dimmer is likely using pulse-width modulation (fast, variable switching) to control the amount of power the LED receives. Someone has mapped the dimmer's PWM's duty cycle (which we interpret as 0 to 100%) to an 8-bit value (which we interpret as 0 to 255).
That made a lot of sense. Yes the binary system is only to tell it can give the amount of combinations, not that it is calculating anything which i might have had stuck in my head. Altough as you said, it can be used to perform computation if it is set up correctly. And it is using PWM, that's correct.

I uploaded this image from my book. It sort of states the "preset" of voltage levels on each 1. So this must be what you mean by "Likewise, it's up to humans to design useful ways to switch voltage levels". So dependent on which connections in the IC that are turned on, it will switch between different voltage levels, which again is determined by the code i am writing, followed by the map of the PWM, that is supported by 8-bits. Btw the image shows the function of a A/D converter. Think i got it now?

#### BobaMosfet

Joined Jul 1, 2009
2,053
Hello,
To start of i want to say that i have basic knowledge in terms of electric/magnetic phenomena. I'm am now learning more about electronics.

I am struggling to understand how the binary numbers can be different bit-values. I'm at this moment programming with the arduino board in my class, and the simple project i am working on is to make a led-light dim, in other words make it have a fade effect. It's quite easy, but to explain my "problem":

Brightness is the integer for the light emitted, and has value between 0-255. The fade-amount integer is set to jump in steps of 5. So as the fade goes up or down, it happens with 5´s between 0-255. The range of 0-255 is what i want to understand. I know the arduino board is an 8-bit microcontroller, and a byte can represent a decimal value of maximum 255, hence the range. Then looking into it, it would mean that different 1's will have different values in them selves, at least when you look at the binary table.

For example the 1st binary 1, would represent decimal value 1, while the 8th binary 1 would represent decimal value 128. So dependent on what value the microcontroller is suppose to send on the output, it will choose between which 1's will be "on"?

Lets say i want 128/255 on the output, the binary would be set to: 10000000.
But if i want 1/255 on the output, the binary would be set to: 00000001.

So the are still just one binary 1 in each code, but obviously they are not by the same value. In this case, what determines these values for the different binary numbers? Yes i want to know the reality of these electrical signals, and how they can vary. My first thought is that they represent different voltage levels, if so, it is then determined by the electric connections in an IC?

I have also read a little about logic gates, registers etc. But not too much.

This is arbitrary, and is base 2. 0 & 1 are the only possible digits to count with. Base 10, has only 10 (0-9), and so forth. Binary was the obvious choice, because it also represents 2 logical states.

• ZimmerJ

Joined Jul 18, 2013
26,347
If you start with programs for Micro processors, learn to program in Assembly, this goes a long way to see how binary math is done.
Max.

• ZimmerJ

#### MrChips

Joined Oct 2, 2009
27,743
Now that we have established that bits and collections of bits can be used to represent anything we choose, we can lay down some ground rules.

1) Boolean Negation
NOT 0 = 1
NOT 1 = 0

2) Boolean OR
0011 OR 0101 = 0111

3) Boolean AND
0011 AND 0101 = 0001

0000 + 0001 = 0001
0001 + 0001 = 0010
:
:
0011 + 0101 = 1000
:
:
1111 + 0001 = 0000

These rules and concepts are virtually universal to all computer systems and apply regardless of what the patterns represent.

• ZimmerJ

#### bogosort

Joined Sep 24, 2011
696
Btw the image shows the function of a A/D converter. Think i got it now?
It'd be more accurate to say that it shows the function of a digital-to-analog converter (DAC). One way of implementing such a thing is to have the output connected to a 5 V voltage source and have the different bit positions switch in/out appropriate resistor values depending on whether the bit was high or low. In this way, the digital high/low signals control a voltage divider to output a specific value.

In any case, I think you have the main idea now. Numbers are a human concept; at the machine level, it's only seeing voltage levels. It's just more convenient for us humans to think of the voltage levels as numbers or, equivalently, strings of "bits".

• ZimmerJ

#### MrChips

Joined Oct 2, 2009
27,743
The rule I presented on addition is only one of many rules.
Here is another scheme:

0000 + 0001 = 0001
0001 + 0001 = 0011
0011 + 0001 = 0010
0010 + 0001 = 0110
0110 + 0001 = 0111
0111 + 0001 = 0101
0101 + 0001 = 0100
0100 + 0001 = 1100
1100 + 0001 = 1101
1101 + 0001 = 1111
1111 + 0001 = 1110
1110 + 0001 = 1010
1010 + 0001 = 1011
1011 + 0001 = 1001
1001 + 0001 = 1000
1000 + 0001 = 0000

• ZimmerJ