Convert Integer to Binary and Store It in an Integer Array of Specified Size:C++

Convert integer to binary and store it in an integer array of specified size:c++

Pseudo code:

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

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

C - Convert Integer to Binary Array

You can parameterize the array by using a pointer to int. It may be useful to parameterize the number of digits as well.

void int_to_bin_digit(unsigned int in, int count, int* out)
{
/* assert: count <= sizeof(int)*CHAR_BIT */
unsigned int mask = 1U << (count-1);
int i;
for (i = 0; i < count; i++) {
out[i] = (in & mask) ? 1 : 0;
in <<= 1;
}
}

int main(int argc, char* argv[])
{
int digit[8];
int_to_bin_digit(40, 8, digit);
return 0;
}

Converting an integer to binary in C

If you want to transform a number into another number (not number to string of characters), and you can do with a small range (0 to 1023 for implementations with 32-bit integers), you don't need to add char* to the solution

unsigned int_to_int(unsigned k) {
if (k == 0) return 0;
if (k == 1) return 1; /* optional */
return (k % 2) + 10 * int_to_int(k / 2);
}

HalosGhost suggested to compact the code into a single line

unsigned int int_to_int(unsigned int k) {
return (k == 0 || k == 1 ? k : ((k % 2) + 10 * int_to_int(k / 2)));
}

Convert int to binary string of certain size

The C standard library does not contain an equivalent function to Integer.toBinaryString(). The good news is, writing such a function won't be too complicated, and if you're in the process of learning C, this problem is fairly ideal for learning how to use the bitwise operators.

You'll want to consult an existing tutorial or manual for all the details, but here are a few examples of the sort of things that would be useful for this or similar tasks. All numbers are unsigned integers in these examples.

n >> m shifts all bits in n right by m steps, and fills in zeros on the left side. So if n = 13 (1101 in binary), n >> 1 would be 6 (i.e. 110), and n >> 2 would be 3 (i.e. 11).

n << m does the same thing, but shifting left. 3 << 2 == 12. This is equivalent to multiplying n by 2 to the power of m. (If it isn't obvious why that is, you'll want to think about how binary numbers are represented for awhile until you understand it clearly; it'll make things easier if you have an intuitive understanding of that property.)

n & m evaluates to a number such that each bit of the result is 1 if and only if it's 1 in both n and m. e.g. 12 & 5 == 4, (1100, 0101, and 0100 being the respective representations of 12, 5, and 4).

So putting those together, n & (1 << i) will be nonzero if and only if bit i is set: 1 obviously only has a single bit set, 1 << i moves it to the appropriate position, and n & (1 << i) checks if that position also has a 1 bit for n. (keeping in mind that the rightmost/least significant bit is bit 0, not bit 1.) So using that, it's a simple matter of checking each bit individually to see if it's 1 or 0, and you have your binary conversion function.

How can I convert a decimal number into binary, saving binary digits in array?

I can't test it right now, but this could help:

// Here there should be the necessary include to enable printing

unsigned int size = 10; // The size of the array that should store the bin output
int a = 3; // The int to be converted to bin

// A function that returns a pointer to a byte, that will be interpreted as an array
unsigned short int
*IntToBin (int input)
{
static unsigned short int output[size]; // Declaring the array to be returned by
// the function, with keyword 'static' to
// allow it to be referenced from outside
for (int i = 0; i < size; i++) {
output[i] = (input >> (size - 1 - i)) & 0x1; // Populating output array by
// bit-shifting the input int
// and doing a bitwise 'and' to
// keep only least significant
// digit
}
return output; // Returning the output array (an array is essentially a pointer,
// so this matches the function declaration)
}

// The main function, that will take care of calling the conversion function and
// printing its output
void
main ()
{
unsigned short int result[] = IntToBin (a); // Assigns the output of the conversion
// function to an array
// Prints the input and output of the conversion function
print("a = ");
print(a);
print("\na in bin = ");
for (int i = 0; i < size; i++) {
print (result[i]);
}
print("\n");
}

But, as mentioned in the comments, the behaviour of a signed int is different than that of an unsigned int. And a length of 10 to your output array is a bit strange.

https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm

How to store individual units of an int in an int array; C Language

Your function is almost correct:

  • you should define the argument type as unsigned to avoid problems with negative numbers
  • you should just return b in the else branch. Trying to use base 10 as an intermediary representation is useless and would fail for numbers larger than 1023.

Here is a corrected version:

int countBits(unsigned d) {
if (d < 2) {
return d;
} else {
return countBits(d / 2) + d % 2;
}
}

There are many more efficient ways to compute the number of bits in a word.

Check Sean Eron Anderson's Bit Twiddling Hacks for classic and advanced solutions.



Related Topics



Leave a reply



Submit