将整数转换为二进制并存储在指定大小的整数数组中:c++ [英] Convert integer to binary and store it in an integer array of specified size:c++

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

问题描述

我想将整数转换为二进制字符串,然后将整数字符串的每一位存储到给定大小的整数数组的元素中.我确信输入整数的二进制表达式不会超过指定数组的大小.如何在 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++?

推荐答案

伪代码:

int value = ????  // assuming a 32 bit int
int i;

for (i = 0; i < 32; ++i) {
    array[i] = (value >> i) & 1;
}

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

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