整型转换为二进制和保存它指定大小的整型数组:C ++ [英] Convert integer to binary and store it in an integer array of specified size:c++

查看:156
本文介绍了整型转换为二进制和保存它指定大小的整型数组:C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲的整数转换为二进制串,然后将整数串的每个比特存储到一个给定大小的整数数组中的一个元素。我相信,在输入整数的二进制前pression不会超过指定数组的大小。如何做到这一点的C ++?

I want to convert an integer to binary string and then store each bit of the integer string to an element of a integer array of a given size. I am sure that the input integer's binary expression won't exceed the size of the array specified. How to do this in c++?

推荐答案

伪code:

int theValue = ????
int i;

for (i = 0; i < 32; ++i) {  // assuming a 32 bit int
    array[i] = theValue & (1 << i) ? 1 : 0;
}

这篇关于整型转换为二进制和保存它指定大小的整型数组:C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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