How do you convert an unsigned long variable into an int array in C?
You would cast anything to an int array by putting `(int*)` in front. However this is dangerous unless you know what you're doing.
Are you sure that the unsigned long is supposed to be the address of an int array?
I start with a given variable andwe're supposed to find how many consecutive 1's are in it's binary form. I figured the easiest way to do that would be to convert it to an array and go from there
I don't think there is any need for that.
``` int count = 0; while (x > 0) { if (x & 1) { count++; } x >>= 1; } ```
The bitwise operators are meant for this.
correct, but will it work like that if we're given a hex number to start?
What? All numbers are being stored as binary numbers.
hex/int are just ways to represent them as a string.
You're converting the ascii string to an integer, right?
starting with a hex number of type unsigned long, would it need to be converted at all besides into binary? I'm a little confused on this one.
No, you're not getting it...
hex, decimal, and binary are ways to write natural numbers. In the computer, the number is actually being stored using binary.
If we are talking about a hex number that was input by a user, then they input it as ascii. When you used scanf or atoi, it was converted into binary. Otherwise it would be stored as a char[].
oh, okay. I just was throwing the code into my compiler and it did work as you said it would. Thanks for helping me. I think i understand it better now.
The only caveat is that this could cause problems with negative numbers due the way bit shifting words..
Join our real-time social learning platform and learn together with your friends!