NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Do not understand this
July 29, 2010 by endrien 
In the PDF it shows these: 11011010  (1<<2) = 11011110 Suppose ADCRA is an 8 bit register... What is ADCSRA now? 11010010 I do not understand how (1<<2) translates to a binary number or even what it means. I that ADCSRA is the Analog digital converter but I don't understand how to figure out what it is given that ADCRA is an 8 bit register. Thanks for any help. 

July 30, 2010 by hevans (NerdKits Staff) 
Hi endrien, Great question. This section in the guide is admittedly throwing you into a lot of new stuff, most of which we dive a little deeper into in later portions of the guide. The notation (1<<2) is a nifty way of creating binary numbers that might seem strange at first but is actually rather convenient. The 1 is just a 1, represented as 1 in binary. It is left shifted by 2 which means it shifts over two places in its binary representation to become 00000100. That then gets OR'd with 11011010. You do a bitwise OR with 00000100, you get 11011110. The important thing to note is that the #2 position is now a 1, and the other bits remain the same. This is a convenient way of setting a single bit of a register to 1 without affecting the other bits, or caring about what the value used to be. The next exercise after that asks you to pretend ADCSRA (a register) has the value 11010010 in it, and asks what the value becomes if you do ADCSRA &= ~(1<<3). If you go through the exercise of bit shifting the one, then inverting the bits, then doing the bitwise and, you will find that it is a convenient way of turning a bit to 0, again without affecting the other bits. Give it a try. Hope that helps. Let me know if something is still unclear. Humberto 
August 12, 2010 by endrien 
That it part of my problem in understanding this, I do not know binary, so I do not understand how you got 00000100. 
August 12, 2010 by hevans (NerdKits Staff) 
Hi endrien, You should definitely try to get a little more familiar with binary as you start to deal with low level embedded systems. Take a look at our bits bytes and binary tutorial for a start on that. It is a tough concept, so give it a little while to sink in. Let me know if you still have questions. Humberto 
August 13, 2010 by Rick_S 
I'm not sure how much you don't know binary. However, it in of itself isn't a mystery, it's how the numbers are maniupulated in a program that becomes quite confusing at times. Binary numbers are just like the base 10 numbers we all grew up with except they are base 2. What that means is there are only 2 numbers (0 & 1) that make up every number. Zero = 0 One = 1 Two = 10 Three = 11 Four = 100 Five = 101 Six = 110 Seven = 111 Etc... I'm sure you see the pattern. The reason binary is so usefule when programming is because in electroncs, you normally think of something being either on or off. This can be represented easily by a binary 1 or 0. Computer memory is often organized in 8 bits (binary digits) or a byte. These bits get represented as a binary number of 00000000 to 11111111 (or 0 to 255 decimal). There are other groupings of bits called nibbles and words but we'll stick to a byte for now as that is the most common. Now when you see (1<<2) All that means is that you take a binary 1 (00000001) and shift that binary digit two places (00000100). If you OR that with another binary number (Because OR () makes the digit one if either the original or the OR'd value is 1), the original number will stay the same except the position of the shift will be guaranteed to be one. Like in your example at the top where you had: 11011010  (1<<2) = 11011110 11011010 (OR) (00000100) = 11011110 Hope that clears it up a bit and didn't make it more confusing :D Rick 
August 13, 2010 by BobaMosfet 
endrien Don't let the term 'register' confuse you. Without getting into how its actually made, just think of it as a single memory location that can hold a specific value. As for binary, binary works just like the math your already familiar with understanding that is the trick. For example, in base 10 (normal decimal base), each digit can only hold values from 0 to 9. What happens when you add a 1 to a 9? Well, it carries. And that takes another digit to the left, doesn't it? Binary is the same. Since in binary (base two), you may only have 0 through 1. The 'base' tells you how many values a single digit can describe in any base. 'decimal' = 10, 'binary' = 2, hexadecimal = 16. (We just say 'hex' for short, when talking about hexadecimal and we'll show you examples of it in a moment, also). Back to binary: Since a single binary digit may only describe the values 0 through 1, that means that if we add a 1 to a 1, it..... (drum roll please)..... you guessed it: It carries. Because it carried over to another digit to the left, 1 + 1 = 2 = '10' in binary (or if working with all 8 bits: 00000010). When we say '<<' or '>>' in the C or C++ programming language, we're telling the compiler to take a given value and shift it left or shift it right. If we shift left, we add zeroes on the right. If we shift right, we add zeroes on the left. Any digits that get shifted off an end disappear (for all intents & purposes). In any base when you shift left you are multiplying by the base. If you shift right, you are dividing by the base. Most of the time, you only see shifting employed in binary but numbers are all the same value, irregardless of base, so it works in any base. Now, for hex, since it's base says it can hold 16 values in a single digit, but we only have numbers (in any base) that go from 0 to 9 we have to also use letters. This can be confusing. Since this stuff was created by Americans, it relies on our alphabet. So once we've gone past 9, we start at 'A', followed by 'B', then 'C', and D, E, and F. So 12 decimal would be 'C' in hex (or more commonly written as '0xC' or '0x0C'. Hope that helps BM 
August 13, 2010 by endrien 
I watched the video and started to understand it, then came back here and got completely lost again. He talks about it completely different. I get that each place is a different power of two, for example the first place is 1, the second is 2 then 4 etc, in where he was explaining it 00000110 would be 4+2 which would = 6. Now how does is that the same as: Zero = 0 One = 1 Two = 10 Three = 11 Four = 100 Five = 101 Six = 110 Seven = 111 
August 14, 2010 by bretm 
Six = 110 (base 2) 6 (base 10) = 00000110 (base 2) 110 and 00000110 are the same, just like $75 is the same is $00075. In decimal nobody leaves on the leading zeros, but we do in binary because those zeros often correspond to actual bits in a memory register that have to be either 0 or 1. 
August 16, 2010 by hevans (NerdKits Staff) 
Hi endrien, Does it make more sense now? I think you were getting thrown off by the leading zeros. Let us know if you are still having problems understanding this. Humberto 
September 11, 2014 by Oldskoolgamer 
Hello, I am a little confused with the bitwise shift operation, I can only get the first example to work if I make a couple of assumptions. 1101 1010 &~ (1<<3). // my first assumption is that I only preform the shit on the first byte, therefor I get 1101 0000. // secondly, I only apply the ~ operator to the shifted bits. 1101 0111. now when I perform the & operation I get the desired result. 1101 1010 & 1101 0111 = 1101 0010 are my assumptions correct, or am I doing something wrong? Thank you John. 
September 11, 2014 by Oldskoolgamer 
You may disregard my previous question, I figured it out. Thank you. John 
September 29, 2014 by BobaMosfet 
Oldskoolgamer No problem. An easier way to write it perhaps is:
Since we're ANDing, only if 1 and 1 exist, does it result in a 1. 
Please log in to post a reply.
Did you know that you can aim and fire a servocontrolled water gun from your computer? Learn more...
