二进制和十进制相互转换、位移运算
发布时间:2020-12-14 04:43:24 所属栏目:大数据 来源:网络整理
导读:二进制和十进制相互转换、位运算 记录下在codewar上做的一个题目和收获 128.32.10.1 == 10000000.00100000.00001010.00000001 Because the above IP address has 32 bits,we can represent it as the unsigned 32 bit number: 2149583361 Complete the funct
二进制和十进制相互转换、位运算记录下在codewar上做的一个题目和收获 128.32.10.1 == 10000000.00100000.00001010.00000001 Because the above IP address has 32 bits,we can represent it as the unsigned 32 bit number: 2149583361 Complete the function that takes an unsigned 32 bit number and returns a string representation of its IPv4 address. Example : 2149583361 ==> "128.32.10.1" 自己的解题思路是将十进制的数转为二进制(不足32位补0),然后依次取8位转化为十进制的数字,再用 里面的几个点记录一下:
let x = 2149583361; x.toString(2) // "10000000001000000000101000000001"
Number.parseInt("10000000001000000000101000000001",2) // 2149583361
let x = 998,//指定值 x_2 = x.toString(2),len = 32 - x_2.length; // 需要补0的个数 x_2 += Array(len + 1).join('0'); 完整解题如下: function int32ToIp(int32){ let int2 = int32.toString(2),len = 32 - int2.length,begins = [0,8,16,24],ipArr = []; if (len) { int2 += Array(len + 1).join('0') } begins.forEach((begin) => { ipArr.push(Number.parseInt(int2.slice(begin,begin + 8),2)) }) return ipArr.join('.'); } int32ToIp(2149583361) // '128.32.10.1' 提交之后发现其他大佬的简洁思路是使用 位移运算符 let x = 2149583361; // 按位移动会先将操作数转换为大端字节序顺序(big-endian order)的32位整数 x >> 24 & 0xff // 128 //右移24位即可得到原来最左边8位,然后&运算取值 同理右移16、8、0即可取到对应的IP字段。 函数如下: function int32ToIp(int32){ return `${int32 >> 24 & 0xff}.${int32 >> 16 & 0xff}.${int32 >> 8 & 0xff}.${int32 >> 0 & 0xff}` } int32ToIp(2149583361) // '128.32.10.1' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |