c - “ans += n << count”是什么意思?
问题描述
// Function for multiplication
int multiply(int n, int m)
{
int ans = 0, count = 0;
while (m)
{
if (m % 2 == 1)
ans += n << count;
// increment of place value (count)
count++;
m /= 2;
}
return ans;
}
那个表达是什么意思?如何以对初学者更友好的形式重写这个表达式?
解决方案
该表达式ans += n << count;
的含义与以下相同:
int n_shifted = n << count; /* bitshift n to the left by count bits */
ans = ans + n_shifted; /* add n_shifted to ans */
推荐阅读
- android - Kotlin:如何用嵌套列表映射列表
- c# - 我有异常错误“System.Data.dll 中出现‘System.Data.SqlClient.SqlException’类型的未处理异常‘ID’附近的语法不正确。”
- c# - 如何获取 Azure Blob 存储中目录的“上次修改时间”属性
- mongodb - ClassNotFoundException:com.mongodb.spark.rdd.partitioner.MongoPartition
- swift - 输入“我的错误?” 不符合协议“错误”
- java - 记录在循环未完成之前不反映在数据库中
- geometry - 极坐标中给定的 2 个向量的叉积的大小
- oracle - 表结构更新后更新表需要更长时间
- javascript - 如何在不发布的情况下在 lerna monorepo 中构建 docker 镜像
- machine-learning - 在 Google 助理的操作中添加自定义词