【问题标题】:What does this operatoer |= mean? [duplicate]这个运算符 |= 是什么意思? [复制]
【发布时间】:2013-07-30 10:12:30
【问题描述】:

在下面的java代码中:

Notification noti = nBuilder.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;

this 运算符 (|=) 是什么?

【问题讨论】:

  • 就像 +=,-= 等
  • |= 按位包含 OR 和赋值运算符 noti.flags |= Notification.FLAG_AUTO_CANCELnoti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL 相同

标签: java android operators


【解决方案1】:
noti.flags |= Notification.FLAG_AUTO_CANCEL;

意思

noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;

在哪里 |是Bit wise OR operator

【讨论】:

    【解决方案2】:

    这是包含赋值运算符的按位或。 扩展为noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL; 同样,&= 表示按位与,^= 表示按位异或,~= 表示按位非。

    【讨论】:

      【解决方案3】:

      按位或,等同于:

      noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;
      

      它对操作数的位执行“或”运算。说你有

      // noti.flags =                      0001011    (11 decimal)
      // Notification.FLAG_AUTO_CANCEL =   1000001    (65 decimal)
      
      // The result would be:              1001011    (75 decimal)
      

      【讨论】:

        【解决方案4】:
        • |是位位还是运算符
        • |= 是 noti.flags = noti.flags | Notification.FLAG_AUTO_CANCEL;

          noti.flags |= Notification.FLAG_AUTO_CANCEL;

          计算noti.flags和Notification.FLAG_AUTO_CANCEL的按位或,并将结果赋给noti.flagsd。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-09
          • 2016-03-07
          • 2012-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-19
          相关资源
          最近更新 更多