c - "*="在C编程中到底是什么意思?

标签 c bitwise-operators

<分区>

我一直在看一些 RobotC 代码,它与 C 非常相似(而且我没有足够的声誉来制作新的 RobotC 标签),我遇到了 *= 运算符。我在谷歌上搜索了很多,但我只能知道它是 C 中的按位运算符。似乎没有人确切地说出它的作用,但是如果你们能提供帮助,我将不胜感激。

rot *= 5;

这是我找到它的代码。所有的功能都是重新调整机器人的方向,使其始终面向北方。

//Turns back to North
void TurnStraight(int cdegree) //cdegree is the sensor value read by the compass sensor
{
  int rot = cdegree % 360;
  int mot = 1;
  //stop when the NXT facing North
  if (cdegree == 0){
     return;
  }
  //reset the encoders value to avoid overflaow
   clear_motor_encoders();

   if (cdegree > 180 && cdegree < 360){
      rot = 360 - rot;
      mot = 0;
   }

   rot *= 5;  // ratio between the circumference of the tire to the circumference of the     rotation circle around itself
   switch (mot){
     case 1:
     moveTo(rot/2,1);
     break;
     case 0:
     moveTo(rot/2,-1);
     break;
     case -1:
     moveTo(rot,1);
     break;
   }
}


void clear_motor_encoders()
{
   nMotorEncoder[motorA] = 0;
}

void moveTo(int rot, int direction)
{
   nSyncedMotors = synchAC;
   nSyncedTurnRatio = -100;
   nMotorEncoderTarget[motorA] = rot;
   motor[motorA] = direction * 50;
   while (nMotorRunState[motorA] != runStateIdle) ;
   motor[motorA] = 0;

}

这当然不是我的代码,我只是想知道它是如何工作的。

最佳答案

相当于:

rot = rot * 5;

它是称为“复合赋值”运算符的运算符家族的一部分。您可以在此处查看它们的完整列表:Compound Assignment Operators (Wikipedia)

请注意 *= 不是按位运算符,因为 * 不是。但是一些复合运算符是按位的 - 例如,&= 运算符是按位的,因为 & 是。

关于c - "*="在C编程中到底是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456599/

相关文章:

c - 如何检查数组的元素是int还是float?

c - 如何为数组单元格中的反向位顺序创建宏

C - 在两个数字之间交换一点

c - 十进制转二进制

c - 努力让 PortAudio 与 MinGW 一起工作

c - 带参数的宏

c - Search a String in a string(字符串由用户输入)

c++ - C语言的溢出工具是什么?

c++ - C++ 中的多数函数使用 3 uint8_t

boolean - 什么是位运算符?