• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Number conversions

Josh123

Diamond Member
Ok so I started the spring semester and I'm currently taking CS 1362. This is the 2nd half of Computer Science 1 and so far we've only gone over number systems and what not.

Anyone good with these conversions and signed/unsigned magnitude storage modes?
 
The 2's complement of 10110 is the main one that is stumping me.
You forgot to mention the little 3 next to 10110. That's 10110 base 3, not base 2. I would take it to base 2, do the 2's complement, then take it back to base 3.

Just IMHO, they are teaching you lots of useless stuff. Nobody uses anything other than 1's complement (~) and 2's complement (-).

Edit: For Lab 8 Page 1, a lot of that is wrong. Consider: an unsigned 4-bit value can store 16 values, 0 through 15, as you correctly stated on Lab 6. Now, a signed 4-bit value can also store 16 values, and only one of those is zero. :whiste:

I also tend to disagree with saying that (unsigned char) or (char)[a large or small number] is "invalid" or "out of range". As an example, (unsigned char)36006925 (your post number) may or may not generate a compiler warning, but it will also return 13. But you know your professor better than I do.
 
Last edited:
I also tend to disagree with saying that (unsigned char) or (char)[a large or small number] is "invalid" or "out of range". As an example, (unsigned char)36006925 (your post number) may or may not generate a compiler warning, but it will also return 13. But you know your professor better than I do.

They aren't learning casting, they're learning simple binary representation. One of the fundamental concepts is that you can only represent a limited range of values.
 
They aren't learning casting, they're learning simple binary representation. One of the fundamental concepts is that you can only represent a limited range of values.

:hmm: I guess I can see that. "0" is not a 1-digit representation of 10, 100, and 1000.
 
You forgot to mention the little 3 next to 10110. That's 10110 base 3, not base 2. I would take it to base 2, do the 2's complement, then take it back to base 3.

Just IMHO, they are teaching you lots of useless stuff. Nobody uses anything other than 1's complement (~) and 2's complement (-).

Edit: For Lab 8 Page 1, a lot of that is wrong. Consider: an unsigned 4-bit value can store 16 values, 0 through 15, as you correctly stated on Lab 6. Now, a signed 4-bit value can also store 16 values, and only one of those is zero. :whiste:

I also tend to disagree with saying that (unsigned char) or (char)[a large or small number] is "invalid" or "out of range". As an example, (unsigned char)36006925 (your post number) may or may not generate a compiler warning, but it will also return 13. But you know your professor better than I do.

On Lab 8 on the first page, wouldn't you only count the first 3 spots since the 4th is the indicator of it being negative or positive?
 
When I did COBOL we had a system for sorting by SSN if you make it numeric and pack the number it takes up 5 digits. We used an even more complicated method. We also rearranged the SSN placing the first 3 digits on the end of the number. This was to speed up sorting. The SSN often starts with 0,1,2,3,4.

This is kind of ancient history.
 
So are you testing the input to insure it is numeric before you store it?

Either that or you may need a conversion process that has error control that handles the error in such a way to keep a program from crashing with an unhandled condition.
 
Last edited:
On Lab 8 on the first page, wouldn't you only count the first 3 spots since the 4th is the indicator of it being negative or positive?

It's not quite that simple. Why don't you try this?

1. For a 4-bit value, start counting 0, 1, 2 in binary, until you get to something that would represent a negative number.
2. What is that number? If you think it's -1, think again. 😉 And remember, -0 only happens in floating point.
3. If you still think Lab 8 is right, try the reverse, counting 0, -1, -2 in binary, until you get to something that would represent a positive number.

Edit: Or even simpler, enumerate all the decimal values of a two-bit signed integer. How many of them are there? Only one is "zero". What are the rest?
 
Last edited:
It's not quite that simple. Why don't you try this?

1. For a 4-bit value, start counting 0, 1, 2 in binary, until you get to something that would represent a negative number.
2. What is that number? If you think it's -1, think again. 😉 And remember, -0 only happens in floating point.
3. If you still think Lab 8 is right, try the reverse, counting 0, -1, -2 in binary, until you get to something that would represent a positive number.

Edit: Or even simpler, enumerate all the decimal values of a two-bit signed integer. How many of them are there? Only one is "zero". What are the rest?

He's correct, actually. The instructions on that page tell them to use a sign magnitude representation, so a N bit number will be able to represent values in the range [-(2^(N-1)-1), 2^(N-1)-1]. However since that representation is a true sign bit you end up with both positive and negative 0, same as floating point.

For two's complement, you're correct. 🙂
 
Back
Top