Ruslan_nig

BitShifting

Dec 27th, 2021
1,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. uint16_t speedMotor = 8349;
  2. uint16_t speedFirstByte=0;
  3. uint8_t speedSecondByte=0;
  4.  
  5. void setup()
  6. {
  7.  Serial.begin(9600);
  8.  
  9.   Serial.print ("DEC = ");
  10.   Serial.println(speedMotor,DEC);  
  11.   Serial.print ("HEX = ");
  12.   Serial.println(speedMotor,HEX);
  13.   Serial.print ("BIN = ");
  14.   Serial.print("\t\t");
  15.   Serial.println(speedMotor,BIN);
  16.  
  17.   //выделяем первый байт, накладываем маску, обнуляем последние 8 бит
  18.   speedFirstByte = speedMotor & 0b1111111100000000;
  19.   Serial.print ("speedFirstByte=");
  20.   Serial.print("\t");
  21.   Serial.println(speedFirstByte,BIN);  
  22.  
  23.   speedFirstByte = speedFirstByte >> 8;
  24.   Serial.print("speedFirstByte BINARY = ");
  25.   Serial.println(speedFirstByte,BIN);
  26.   Serial.print ("speedFirstByte HEX = ");
  27.   Serial.println(speedFirstByte,HEX);
  28.  
  29.   //выделяем второй байт, накладываем маску, обнуляем первые 8 бит
  30.   speedSecondByte = speedMotor & 0b0000000011111111;
  31.  
  32.   Serial.print("speedSecondByte BINARY = ");
  33.   Serial.println(speedSecondByte,BIN);
  34.   Serial.print ("speedSecondByte HEX = ");
  35.   Serial.println(speedSecondByte,HEX);
  36. }
  37.  
  38.  
  39. void loop()
  40. {
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment