Javascript - 将整数转换为位数组 [英] Javascript - convert integer to array of bits

查看:139
本文介绍了Javascript - 将整数转换为位数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript将一个整数(我知道将在0到32之间)转换为0和1的数组。我环顾四周但找不到合适的东西..

I am trying in javascript to convert an integer (which I know will be between 0 and 32), to an array of 0s and 1s. I have looked around but couldn't find something that works..

所以,如果我有一个22的整数(二进制10110),我想访问它:

So, if I have an integer as 22 (binary 10110), I would like to access it as:

Bitarr[0] = 0
Bitarr[1] = 1
Bitarr[2] = 1
Bitarr[3] = 0
Bitarr[4] = 1

有什么建议么?
非常感谢

Any suggestions? Many thanks

推荐答案

转换为基数2:

var base2 = (yourNumber).toString(2);

访问字符(位):

base2[0], base2[1], base2[3], etc...

这篇关于Javascript - 将整数转换为位数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆