uas_arduino

Bit shifter

Mar 22nd, 2013
44
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdarg.h>
  2.  
  3. void printf(char *fmt, ... ){
  4.         char tmp[128]; // resulting string limited to 128 chars
  5.         va_list args;
  6.         va_start (args, fmt );
  7.         vsnprintf(tmp, 128, fmt, args);
  8.         va_end (args);
  9.         Serial.print(tmp);
  10. }
  11.  
  12. void setup(){
  13.   Serial.begin(9600);
  14. }
  15.  
  16. void loop(){
  17.   byte a = 0x55;
  18.   byte b = 0x79;
  19.   Serial.print("A = 0x");
  20.   Serial.println(a,HEX);
  21.   Serial.print("Left shifted by one: 0x");
  22.   Serial.println((a << 1),HEX);
  23.   Serial.print("Right shifted by one: 0x");
  24.   Serial.println((a >> 1), HEX);
  25.   Serial.print("0x");
  26.   Serial.print(a,HEX);
  27.   Serial.print(" AND 0x");
  28.   Serial.print(b,HEX);
  29.   Serial.print(" = 0x");
  30.   Serial.println((a & b),HEX);
  31.   while(1){}
  32.   printf("Left shifted by one: %02x\n",(a << 1));
  33.   printf("Right shifted by one: %02x\n",(a >> 1));
  34.   printf("%02x AND %02x = %02x\n", a, b, (a&b));
  35.  
  36. }
RAW Paste Data