By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

Changing sign of an integer.

P: 7
Hi,

What i'm trying to accomplice is to change the sign of an integer value. Not do the 2 complement so I get the negative value, but to change sign of something that already has done the 2 complement but has wrong sign nonetheless.

Expand|Select|Wrap|Line Numbers
  1. int temp = 123456;
  2. temp |= 0x80000000;
The above gives an error that I try to convert from long to integer.

// Andreas
Oct 3 '07 #1
Share this Question
Share on Google+
8 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
What i'm trying to accomplice is to change the sign of an integer value. Not do the 2 complement so I get the negative value, but to change sign of something that already has done the 2 complement but has wrong sign nonetheless.
I have no idea what that means. Could you try explaining again? If the 2's complement has been done, why does it have the wrong sign?
Oct 3 '07 #2

Expert 10K+
P: 11,448
Your question isn't quite clear to me either but, assuming the size of an int and
an unsigned int are equal, you could do this:

Expand|Select|Wrap|Line Numbers
  1. unsigned int signbit= (~0)^(~0>>1);
  2. ...
  3. unsigned int flipsign(unsigned int x) { return x^signbit; }
  4.  
kind regards,

Jos
Oct 3 '07 #3

P: 7
I have no idea what that means. Could you try explaining again? If the 2's complement has been done, why does it have the wrong sign?
I get a binary value represented by 25 bits. Where the 25:th bit is the sign for now, so I need to move that to the 32:nd bit if possible so I get the correct sign. I.e it has a span between 0 to 16.777.215 and -1 to -16.777.216 I think it becomes.

i.e if I get the value 16.777.217 I wan't to get it as -16.777.216 (+/- some)
Oct 3 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
I get a binary value represented by 25 bits.
How is this represented?
Oct 3 '07 #5

P: 7
It represents a signed integer in binary only that it is 7 bits less, and that instead of bit 32 being the sign its bit 25.
-16.777.216 to 16.777.215 only that I have the value after reading it out as a integer so I have a span between 0 and 33.554.431 and I need to see if bit 25 is 1 I have to move that to set the 32:nd bit to get the correct value.
Oct 4 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
It represents a signed integer in binary only that it is 7 bits less, and that instead of bit 32 being the sign its bit 25.
-16.777.216 to 16.777.215 only that I have the value after reading it out as a integer so I have a span between 0 and 33.554.431 and I need to see if bit 25 is 1 I have to move that to set the 32:nd bit to get the correct value.
Hold on. Most negative values are represented in 2's complement so moving a sign bit may not cut it.

What is the type of this 25-bit integer? Are you using a bitfield or are you using an unsigned int to hold it.

If you use an unisgned int, I would set the sign bit to zero right where it's located. Then move the unsigned int to an int. Then, if the sign (that was zeroed) was originally 1, I would multiply the int by -1 to create the correct 2's complement representation.
Oct 4 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
If you use an unisgned int, I would set the sign bit to zero right where it's located. Then move the unsigned int to an int. Then, if the sign (that was zeroed) was originally 1, I would multiply the int by -1 to create the correct 2's complement representation.

--------------------------------------------------------------------------------
Nah. That's not going to work. If it was 1, then the bits are already in two's complement. So, you set all bits of an int to 1 andd then replace the first 25 bits with your value.

What do you think?
Oct 4 '07 #8

P: 7
I found a solution to this, althoughn not fancy. It currently is in an signed integer so I just checked if the 25 bit was set and then withdrew it from 0x1000000 and changed sign.
Oct 5 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.