Ask your own question, for FREE!
Computer Science 25 Online
OpenStudy (anonymous):

How do you convert an unsigned long variable into an int array in C?

OpenStudy (anonymous):

You would cast anything to an int array by putting `(int*)` in front. However this is dangerous unless you know what you're doing.

OpenStudy (anonymous):

Are you sure that the unsigned long is supposed to be the address of an int array?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

I don't think there is any need for that.

OpenStudy (anonymous):

``` int count = 0; while (x > 0) { if (x & 1) { count++; } x >>= 1; } ```

OpenStudy (anonymous):

The bitwise operators are meant for this.

OpenStudy (anonymous):

correct, but will it work like that if we're given a hex number to start?

OpenStudy (anonymous):

What? All numbers are being stored as binary numbers.

OpenStudy (anonymous):

hex/int are just ways to represent them as a string.

OpenStudy (anonymous):

You're converting the ascii string to an integer, right?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

No, you're not getting it...

OpenStudy (anonymous):

hex, decimal, and binary are ways to write natural numbers. In the computer, the number is actually being stored using binary.

OpenStudy (anonymous):

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[].

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

The only caveat is that this could cause problems with negative numbers due the way bit shifting words..

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!