-3

I have a program that will take two 4-byte integers as input and I need to store these into integer arrays like so...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[]) {
    int vals1[32], vals2[32];
    int num1 = atoi(argv[1]);
    int num2 = atoi(argv[2]);

    // so argv[1] might be 47 and I would want to set the values of the vals1 array to reflect that in binary form
}

Any suggestions?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Hints: for a positive number, '% 2' extracts the least significant binary digit, and `/ 2` removes the final binary digit. – Bathsheba Mar 08 '19 at 16:02
  • Your question is unclear. So is your input a string? Do you want to just store that as an int into an int array? Do you want a binary representation of your int? What representation? – Superlokkus Mar 08 '19 at 16:02
  • What have you tried so far? Where specifically are you stuck? – Govind Parmar Mar 08 '19 at 16:03
  • @Superlokkus, i am taking a cstring which i will cast to an integer using the atoi function. So, i start with "47" and turn it into the integer 47, but then want to extract the binary bits that represent 47 as an integer and store them in val1 – Ben deVries Mar 08 '19 at 16:59
  • `int nthbit(unsigned long long x, int n) { return (x >> n) & 1; }` – pmg Mar 08 '19 at 17:24

1 Answers1

1

First task would be to convert a char * to an int, which you said you can. So here comes the next part i.e. getting the binary representation. For getting the binary representation of any data type, usage of Shift Operator is one of the best ways. And you can get it by performing shift on the data type and then performing Bitwise AND i.e. & with 1. For example, if n is an integer

int n = 47;
for (int i = 0; i < 32; i++)
{
   /* It's going to print the values of bits starting from least significant. */
   printf("Bit%d = %d\r\n", i, (unsigned int)((n >> i) & 1));
}

So, using shift operator, solution to your problem would be something like

void fun(int n1, int n2)
{
    int i, argv1[32], argv2[32];

    for (i = 0; i < 32; i++)
    {
        argv1[i] = ((unsigned int)n1 >> i) & 1;
        argv2[i] = ((unsigned int)n2 >> i) & 1;
    }
}

You have to be careful about the bit order i.e. which bit are being stored at which of the array index.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Mazhar
  • 575
  • 5
  • 20
  • 1
    `n1 >> i` has implementation defined behavior if `n1` is negative. You should use `(unsigned int)n1 >> i` – chqrlie Mar 08 '19 at 17:17
  • Like what exactly what happen if n1 is negative for example, please elaborate a bit more. Thanks a lot. – Mazhar Mar 08 '19 at 17:20
  • 1
    @Mazhar [This](https://stackoverflow.com/questions/1857928/right-shifting-negative-numbers-in-c) explains it. – Michail Mar 08 '19 at 17:35
  • Depending on the architecture, the sign bit may be replicated or set to 0 or anything else might happen as long as it is specified for the target architecture. Just avoid right shifting signed integers that may have negative values. – chqrlie Mar 08 '19 at 17:35
  • 1
    Another point: you make the silent assumption that `sizeof(int) == 4` because you use `for (i = 0; i < (8 * sizeof(int)); i++)` and the sizes have a fixed size of `32`. Better write `for (i = 0; i < 32; i++)` – chqrlie Mar 08 '19 at 17:38
  • 1
    I took the liberty to fix the parentheses in the answer's code: `n1` must be cast before the `>>` operation. – chqrlie Mar 08 '19 at 18:00
  • Thanks for making it meaningful. – Mazhar Mar 08 '19 at 18:05