<- back to main Article

BitwiseOperations

For better understanding how to manipulate single Bits...

/**************************************************************************************************
*
*   bitwiseOperations_00
*
*   Version:      00 - Mai 2010
*   Author:       Tom Pawlofsky www.caad.arch.ethz.ch tom-DOT-pawlofsky-AT-arch-DOT-ethz-DOT-ch
*
*   Desc:         show how to set single bits of a Byte
*
***************************************************************************************************/

/* some nice links
http://www.arduino.cc/playground/Code/BitMath
*/


void setup(){
        byte a; // 00000000
        byte b;
        byte c;
        byte d;

        // start Serial communication
        Serial.begin(9600);
        Serial.println("bitwise Examples");

        // example how to create 00000100------------------------------------------EXAMPLE-1------------------------------------
        Serial.println("create 00000100");
        a = B00000100; // use arduino constant
        b = (1<<2);    // use bitshit - shift 1 by 2 positions to left
        c = 4;         // use the decimal equivalent of binary 100
        bitSet(d,2);   // use the bitSet function of arduino, note: the bits are indexed from right to left, 0-based
        // bitSet is similar to the sbi() macro you sometimes find if Registers of the microController are accessed

        Serial.println(a,BIN);
        Serial.println(b,BIN);
        Serial.println(c,BIN);
        Serial.println(d,BIN);




        // example how to combine multiple Bytes to create 10101010----------------EXAMPLE-2------------------------------------
        Serial.println("create 10101010");
        a = (1<<7) | (1<<5) | (1<<3) | (1<<1); // combining 10000000 , 00100000, 00001000 , 00000010 with bitwise OR

        Serial.println(a,BIN);


        // example how to modify only 3 places of one Byte-------------------------EXAMPLE-3------------------------------------
        // try to change only position 1..3
        // create xxxx111x
        byte mask; // see also http://www.arduino.cc/en/Tutorial/BitMask
        byte modify;
        Serial.println("create xxxx101x");
        a = B10000011; // 100000 11
        b = B10001111; // 1000 1111
        c = B10111111; // 10 111111
        d = B11111111; // 1 1111111   just some "0-1-grafic"

        mask =   B00001110;
        modify = B00001010; // you can change modify to B0000xxx0 to play with

        // clear bit 1..3 with bitwise math using a mask
        a &= ~mask;  // sets all bit s to 0 that are 1 in mask
        // set a equal(=) to the bitwiseAND(&) of old a and bitwiseNOT(~) of mask
        //
        a |= modify; // combine a bitwiseOR modify

        // do it in one line
        b = (b & ~mask) | modify;
        c = (c & ~mask) | modify;
        d = (d & ~mask) | modify;

        Serial.println(a,BIN);
        Serial.println(b,BIN);
        Serial.println(c,BIN);
        Serial.println(d,BIN);

}

void loop(){
}


<- back to main Article

This website has been archived and is no longer maintained.