User:Fabien Labeyrie/Bit Shifting: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "__NOTOC__ __NOEDITSECTION__ ==<span style="color:#00FF00">Bit Shifting : learning how to count like a machine</span>== The idea is to work with inputs and outputs of Arduino to m...")
 
No edit summary
Line 1: Line 1:
__NOTOC__
__NOTOC__
__NOEDITSECTION__
__NOEDITSECTION__
==<span style="color:#00FF00">Bit Shifting : learning how to count like a machine</span>==
==<span style="color:#00FF00">Bit Shifting : how i learned to stop worrying and love binary numbers</span>==
The idea is to work with inputs and outputs of Arduino to make a Simon says kind of game.
Here is how to deal with binary numbers when you are programming on an old computer (Basic V 2.0 on Commodore 64) or when you are configuring internal parameters on your arduino board. Thanks to Mr. Stock who made this nightmare look nearly simple.


<br />
<br />
==How does it work ?==
==What is << (bit shifting) ?==
====Technologies involved====
In the binary numeral system, everything looks like 0110010100010100111... <br />
[http://en.wikipedia.org/wiki/Arduino Arduino]
A bit shift consists in moving one or several digits in a line of 0110010100010100111... <br />
 
====Steps====
#We need a board with a push button and a speaker.
#The code uses two ''while'' loops, constantly checking if the button is pressed, for how long, and launching a timeout during the waiting loop, in order to play the sequence after 5 seconds of wait.


====Examples====
<br />
<br />
==Source code==
1 << 3
<pre>
1 = 0001 in the binary numeral system
1 << 3 means we are gonna move the value 1, to the left <<, 3 times.
1 << 3 : 0001  ->  0010  ->  0100  ->  1000 
              1st move  2nd move  3rd move


<source lang="C">
1 << 3 = 1000 and 1000 in the binary numeral system = 8
const int buttonPin = 7;
</pre>


int buttonState = 0;
7 << 5
int value1;
<pre>
int value2;
7 = 00000111
int interval;
7 << 5 : 0000 0111  ->  0000 1110  ->  0001 1100  ->  0011 1000  ->  0111 0000  ->  1110 0000
int pattern[100];
int i = 0;
int j = 0;
int wfp_start;


void setup() {
7 << 5 = 1110000 in the binary numeral system = 224
  // 9600 is the frequency that will be used by the monitor
</pre>
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}


void loop(){
<br />
=The best is yet to come=
  while (1) {
==The nibble==
   
==Big endian Vs little endian==
    // State : WAIT FOR PRESS
==Logical operations==
    wfp_start = millis();
==Bits and modulo==
 
    while (digitalRead(buttonPin) == LOW && (millis() - wfp_start < 5000 )) {
      // Do nothing the button isn't pressed
    }
 
    // Act as a timeout. Above 5 sec, we exit the WAIT loop and PLAY
    if ((millis() - wfp_start >= 5000 )) break;
   
    // State : WAIT FOR RELEASE
    value1 = millis();
   
    while (digitalRead(buttonPin) == HIGH) {
      // Play tone when the button is pressed
      tone(8, 100, 5);   
    }
 
    value2 = millis();
    interval = value2 - value1; 
    pattern[i]= interval;
    Serial.println(pattern[i]);
   
    // This will be used to simplify the values stored in the array   
    if (pattern[i] < 100) pattern[i] = 100;
    else if (pattern[i] > 99 && pattern [i] < 200) pattern[i] = 200;
    else if (pattern[i] > 199 && pattern [i] < 300) pattern[i] = 300;
    else pattern[i] = 400;
 
    Serial.println(" pattern : ");
    Serial.println(pattern[i]); 
    i++;
   
  }
 
    // State : PLAY
    while (j < i) {
      tone(8, 100, pattern[j]);
      Serial.println("Pattern plays");   
      j++; 
    } 
}
 
</source>

Revision as of 02:00, 24 March 2011


Bit Shifting : how i learned to stop worrying and love binary numbers

Here is how to deal with binary numbers when you are programming on an old computer (Basic V 2.0 on Commodore 64) or when you are configuring internal parameters on your arduino board. Thanks to Mr. Stock who made this nightmare look nearly simple.


What is << (bit shifting) ?

In the binary numeral system, everything looks like 0110010100010100111...
A bit shift consists in moving one or several digits in a line of 0110010100010100111...

Examples


1 << 3

1 = 0001 in the binary numeral system
1 << 3 means we are gonna move the value 1, to the left <<, 3 times.
1 << 3 : 0001  ->  0010  ->  0100  ->  1000   
               1st move  2nd move  3rd move

1 << 3 = 1000 and 1000 in the binary numeral system = 8

7 << 5

7 = 00000111
7 << 5 : 0000 0111  ->  0000 1110  ->  0001 1100  ->  0011 1000  ->  0111 0000  ->  1110 0000 

7 << 5 = 1110000 in the binary numeral system = 224


The best is yet to come

The nibble

Big endian Vs little endian

Logical operations

Bits and modulo