Sign in to follow this  

Hex in Two's Compliment.

This topic is 2486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was just wondering if there is any possible way in c++ to represent a hex number in two's compliment. By this I mean.
Say i have a hexadecimal value of 7FF, or 111 1111 1111 in binary. I need to represent this in two's compliment so I can get a
value of -1 instead of a value of 2046.
If I didn't make myself clear just let me know and I'll try to explain what I mean better.

Thanks.

Share this post


Link to post
Share on other sites
If I understand what you're asking, then read in the hex number as an unsigned int and reinterpret_cast it as a signed int, assuming that you're on a platform where the integer representation of a signed int is twos complement (which should be pretty much every common platform and most of the goofy ones as well).

Share this post


Link to post
Share on other sites
You could probably be more clear. An int datatype is "a hex number in two's compliment", so I think you either mean something else or you don't really understand what you mean.

7FF is what... a string? an int? How are you inputting it, and what do you want to output as? Are we talking about strings of ascii characters?

You can't just say "7FF in two's compliment", without knowing how wide the number is. Is this some hypothetical number type that is 11 bits wide? If we're talking about int datatypes, than 7FF is just 7FF, because the number is really "00000000 00000000 00000111 11111111" . That's not a negative number in two's compliment notation.

Share this post


Link to post
Share on other sites
[quote name='karwosts' timestamp='1298258050' post='4776877']
You could probably be more clear. An int datatype is "a hex number in two's compliment", so I think you either mean something else or you don't really understand what you mean.

7FF is what... a string? an int? How are you inputting it, and what do you want to output as? Are we talking about strings of ascii characters?

You can't just say "7FF in two's compliment", without knowing how wide the number is. Is this some hypothetical number type that is 11 bits wide? If we're talking about int datatypes, than 7FF is just 7FF, because the number is really "00000000 00000000 00000111 11111111" . That's not a negative number in two's compliment notation.
[/quote]

I simply stated if I was being clear enough I would clear it up.
What I meant is i have a Hex number 7FF. I also stated that I didn't want it to output 2046, instead I want it to output -1.
Therefore it is stored as an int such as
int a = 0x7FF;

Thanks for the reinterpret_cast though sicrane. I'm trying to figure it out as we speak.

Share this post


Link to post
Share on other sites
I don't think reinterpret cast will help you, 2046 is still 2046 whether it's an int or an unsigned int.

You'll likely need to implement the logic rules yourself, because an 11-bit number is not a real datatype.

Share this post


Link to post
Share on other sites
My recommendation is to do a 'sign extend'. Basically do what SiCrane and read the number as an unsigned int. Then check the bit that you consider to be the 'sign'. If the sign bit is set then set every bit after the sign to 1. Then reinterpret_cast the result to an int and it should work. I actually wrote a program to do this but this could be homework or something so I don't want to just hand it over.

Share this post


Link to post
Share on other sites
Yes it is for an assignment sort of so I would like to figure it out myself but I do got a question. Do you know where I can find any information on doing 'sign extends'?

Share this post


Link to post
Share on other sites
[quote name='l jsym l' timestamp='1298264096' post='4776904']
Yes it is for an assignment sort of so I would like to figure it out myself but I do got a question. Do you know where I can find any information on doing 'sign extends'?
[/quote]

Yes, read my previous post :). I cover the basic steps needed to do this, are you confused on any of them in particular? That said, I came up with a simpler way to do what you want that you can do manually without much effort:

1. If hex number is negative continue on!
2. Take the 2's complement of number (to make it positive).
3. Output '-' and then output result of step 2.

Basically a negative number can be printed by turning it positive first.

Share this post


Link to post
Share on other sites
In this case if the number of bits in the integer is always 11 (which it basically has to be or how else would you do this) then it's a matter of either ORing it with 0xFFFFF800 or not (assuming a 32-bit int).

Share this post


Link to post
Share on other sites
The operation y = -x for a two's complement system is the same as the arithmetic logic operation y = NOT(x) + 1.

Naturally, you will be limited by the "native" integer sizes supported on the machine, hence x could be either an 8, 16, 32 or 64-bit integer on most mainstream CPUs.

That said, 11-bit two's complement arithmetic is not that much different, except you will have to sign extend the 11-bit value into the native integer size on the machine. Basically this involves either setting or clearing the remaining upper bits for the native integer, depending on what the MSB is for the 11-bit word. If the MSB is 1, then we fill the remaining upper bits with 1, otherwise we clear them with 0.

Here is an illustrated example of an 11-bit word (7FF) superimposed over 16-bit integer, and its MSB highlighted in red:

xxxxx[b][color="#ff0000"]1[/color][/b]11 11111111

If you want to sign extend the two's complement 11-bit integer, the upper xxxxx bits should be either 11111 or 00000, depending on whether the MSB is 1 or 0 respectively. The same rules applies for bigger native integer sizes, except you will need to set or clear more upper bits.

So you homework is:
[list=1][*]Devise a test for the MSB in the 11-bit word;[*]Find a method to set upper bits if MSB == true;[*]Likewise, clear upper bits if MSB == fasle.[/list]The operative word(s) here is "bit masking". iMalc has already given you a hint how you can do point 2, albeit you can do better by not limiting yourself for a specific integer size, such as 32-bits.

Share this post


Link to post
Share on other sites

This topic is 2486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this