加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Code War每天一练第二天

发布时间:2020-12-14 04:25:21 所属栏目:大数据 来源:网络整理
导读:Given an array of one‘s and zero‘s convert the equivalent binary value to an integer. Eg: [0,1] is treated as 0001 which is the binary representation of 1. ? Examples: 将字符数组转化成整数Testing: [0,1] == 1Testing: [0,1,0] == 2Testing:

Given an array of one‘s and zero‘s convert the equivalent binary value to an integer.

Eg: [0,1] is treated as 0001 which is the binary representation of 1.

?

Examples:



将字符数组转化成整数Testing: [0,1] ==> 1 Testing: [0,1,0] ==> 2 Testing: [0,1] ==> 5 Testing: [1,1] ==> 9 Testing: [0,0] ==> 6 Testing: [1,1] ==> 15 Testing: [1,1] ==> 11
res = x

n

+x

n-1

+x

n-2

.......x

1

+x0



#include <stddef.h>

unsigned binary_array_to_numbers(const unsigned *bits,size_t count) {
  unsigned res = 0;
  for (size_t i = 0; i < count; i++)
    res = res << 1 | bits[i];
  return res;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读