5

I recently read this piece of code and I dont know what is the use of "<<=" in the for loop:

for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit

This is the entire code:

//Created August 23 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc

#include <ctype.h>

#define bit9600Delay 100  
#define halfBit9600Delay 500
#define bit4800Delay 188 
#define halfBit4800Delay 94 

byte rx = 6;
byte tx = 7;
byte SWval;

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  delay(2);
  digitalWrite(13,HIGH); //turn on debugging LED
  SWprint('h');  //debugging hello
  SWprint('i');
  SWprint(10); //carriage return
}

void SWprint(int data)
{
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit
     digitalWrite(tx,HIGH); // send 1
    }
    else{
     digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit9600Delay);
  }
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit9600Delay);
}

int SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); 
    delayMicroseconds(bit9600Delay);
    return val;
  }
}

void loop()
{
    SWval = SWread(); 
    SWprint(toupper(SWval));
}
4

As stated in the other answers, the <<= operator shifts the byte to the left; however, the number '1' after it specifies how far to shift it - so, 2 <<= 2 == 8. Possibly clearer in binary:

00000001 <<= 1 == 00000010

00000010 <<= 1 == 00000100

00000001 <<= 2 == 00000100

| improve this answer | |
7

In simpler terms mask <<= 1 is shorthand for mask = mask << 1

In general

variable operator= operand

is the augmented assignment operator which means

variable = variable operator operand 
| improve this answer | |
4

<< is the left bit-shift operator. That means it takes the binary contents of a number and moves everything left by a certain number of binary places (1 place in this case). <<= simply combines the left bit-shift with an assignment, so it takes the current value of mask, shifts it left, and then stores the result back in mask.

In this situation, the mask variable starts with a value of 1. The second time round the loop, it will be 10 (binary). Next time, it will be 100, and so on. Since mask is a byte (which is 8 bits), it will go all the way up to 10000000 and then stop. After that, the 1 effectively falls-off the left and mask ends up with a value of 0.

The purpose of this in the code you provided is to test each individual bit within the data parameter. It basically goes through from least- to most-significant and checks if each bit is 1 or 0. It will then pull an output pin HIGH or LOW (respectively) to match. This allows it to send the contents of the variable digitally to some other device.

Doing low-level binary (aka bit-wise) operations like this is very common in microcontroller programming. If you're not familiar with it then I'd definitely recommend looking up some tutorials online. It opens up a huge range of new possibilities for your projects.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.