极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10850|回复: 2

哪位能帮我仔细解释下红色代码的意思吗?

[复制链接]
发表于 2016-5-20 11:55:42 | 显示全部楼层 |阅读模式
/////////////////////////////////////////////////////////////
int getIRKey() {
  int data[BIT_PER_BLOCK];
  int i;
  while(pulseIn(irPin, HIGH) < start_bit); //Wait for a start bit

  for(i = 0 ; i < BIT_PER_BLOCK ; i++)
    data = pulseIn(irPin, HIGH); //Start measuring bits, I only want HIGH pulses

  delay(100);  
  for(i = 0 ; i < BIT_PER_BLOCK ; i++) //Parse them
  {   
    if(data > bin_1) //is it a 1?
      data = 1;
    else
    if(data > bin_0) //is it a 0?
      data = 0;
    else
      return -1; //Flag the data as invalid; Return -1 on invalid data
  }
  //based on NEC protocol, command data started from bit 16
  //and end with bit 24 (8 bits long)
  int result = 0;
  for(i = 16 ; i < 24; i++) {
    DigiKeyboard.print(data); //print out the value of button in binary form
    if(data == 1) result |= (1<<i-16);
  }  
  return result; //Return key number

}
回复

使用道具 举报

 楼主| 发表于 2016-5-20 21:36:03 | 显示全部楼层
1<<i-16   这个是吧字符型转成int型吗?
回复 支持 反对

使用道具 举报

发表于 2016-5-21 10:06:58 | 显示全部楼层
左移运算的两个操作数应为整数类型。第一个操作数是要进行移位操作的数,第二个操作数指定第一个操作数移动的位数。如果第二个操作数等于0则不发生任何移位。
应用举例:
一,问:计算表达式14 << 2的值。
答:表达式14 << 2的值为56,因为14(即二进制的00001110)向左移两位等于56(即二进制的00111000)。
二,问: 计算表达式8 >> 2的值。
答:表达式8 >> 2的值为2,因为8(即二进制的00001000)向右移两位等于2(即二进制的00000010)。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-20 17:28 , Processed in 0.040738 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表