## simple question (signed ints)

This is the place for queries that don't fit in any of the other categories.

### Re: simple question (signed ints)

Are those what you wanted?
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: simple question (signed ints)

not sure if ov( -250, 1 ) is right, but ov( -128, 1 ) is definately wrong...

other than those, everything's fine.

EDIT:
tested in calculator:
-250 should be -122
so yep, it right

EDIT2:
ov( -128, 1 ) is partially correct since ov( 128, 1 ) is 0 and the equation doesn't know the difference between them

Tcll

Posts: 107
Joined: Wed Jan 01, 2014 6:36 pm

### Re: simple question (signed ints)

If ov(-250, 1) should be -122, ov(-128, 1) should be 0.
If ov(-128, 1) should be -128, ov(-250, 1) should be -121.

I'm guessing you'll need some simple adjustments to get the result you want.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: simple question (signed ints)

the confusion is the two's complement, where 0xFF is -1 instead of -0

what you've provided only allows up to -127 (0x81) for a signed 8-bit int (0x79 is 127, 0x80 is -128, and 0x00 is of course 0)

EDIT:
looks like my last working example was right after all... heh

I'll take it over to daniweb for optimization... I think I've caused you enough stress here...
sorry

Tcll

Posts: 107
Joined: Wed Jan 01, 2014 6:36 pm

### Re: simple question (signed ints)

Maybe check out the link in my signature some time, to find out how to stress less and get your answers faster.
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: simple question (signed ints)

Kebap wrote:Maybe check out the link in my signature some time, to find out how to stress less and get your answers faster.

nice article...
but in my defense (not saying what I did here was right in my approach), I'm autistic,
and typically don't think of alot of these things when trying to describe my issue...

honestly...
I thought it already would blow right through as I thought this was a well known area in binary programming... so that was my fault

to top things off, I usually am not sure how to explain something appropriately until after I have the answer...
(IK this causes alot of confusion on both of our ends, but it's like... I'd have to speak with my future self to know how to explain it now)

sorry I can be such a bother at times
I try not to be, and do work on it quite often... but I have my moments...

thanks for caring though

Tcll

Posts: 107
Joined: Wed Jan 01, 2014 6:36 pm

### Re: simple question (signed ints)

No clue what is going on in this thread:

You want signed ints that overflow back to negative or not?
Code: Select all
`def overflow(number, signed=True, bits=8):    bit_mod = 2**bits    signed_mod = bit_mod//2    if signed:        return (number+signed_mod)%bit_mod-signed_mod    else:        return number%bit_mod            if __name__ == "__main__":    print(overflow(127))    print(overflow(128))    print(overflow(-250))`
Code: Select all
`127-1286`

-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: simple question (signed ints)

Tcll wrote:did a little renaming and reconfiguring

>>> ov( -250, 1 )
-122

>>> ov( 200, 1 )
72
>>> ov( -128, 1 )
-128
>>> ov( 128, 1 )
0
>>> ov( 127, 1 )
127

would what's in yellow be correct??

here's the code:
Code: Select all
`def ov( value, byte_size ):    sign = False    if value<0: value += 1; sign = True    value = abs( value ) & ((1<<((byte_size<<3)-1))-1)    if sign: value = (value*-1)-1    return value`

EDIT:
a little math on 200:
11001000 & 01111111 = 72
so yes bs8( 200 ) is 72

this post already got it,
and I optimized it to:

Code: Select all
`def ov(value,byte_size):    sign = value<0    return (((-1)**sign)*(abs(value+sign)%(1<<((byte_size<<3)-1))))-sign`

it works perfectly

though now I have to ask if what I'm doing should really be considered "proper"?? >.>
since what I'm doing is basically the same thing as masking, but I'm carrying the sign-bit...

Tcll

Posts: 107
Joined: Wed Jan 01, 2014 6:36 pm

### Re: simple question (signed ints)

The question is ambiguous because -250 doesn't have a well-defined bit pattern. Most modern computer architectures use 2's complement binary format which doesn't have a sign bit. In 8 bit 2's complement binary format, you can store the numbers -128 to 127 (note there is one more negative value than positive value). Older computer occasionally used 1's complement binary format which uses a sign bit. In 8 bit 1's complement binary format, you can store the numbers -127 to -0, and 0 to 127 (note there are two versions of 0).

Assuming -250 is stored in 16 bit 2's complement binary format, the bit pattern is: 1111111100000110. If you just keep the last 8 bits (which is a completely valid approach to "down-sizing" a longer number, you'll get 6. But since there are other approaches that can be used, you'll need to decide what you are really trying to do.

casevh
casevh

Posts: 114
Joined: Sat Feb 09, 2013 7:35 am

### Re: simple question (signed ints)

casevh wrote:Most modern computer architectures use 2's complement binary format which doesn't have a sign bit.

o.o
that's not what I read when I designed the signed-int r/w functions in dev4x of my program, even though I didn't consider it... >_>
oh where is that reference... <.<
(will edit when I find it)

yea, the reason I'm so up-tight about these standards is because I'm re-evaluating everything.
dev5 introduces a pointer system which needs all this extra care...

sure I could just make it throw an error, but my rep aims to step things to the next level.

in dev4x, since the input size was known, it was easy to just add a half-size value to the number and break down the binary.
but in this system, the way we need to write the value would require supplying an additional byte_size before we could break the value down properly.
so I used the next best approach, which was getting the mantissa and sign-bit, and fitting the mantissa in the new bit-width and applying the sign-bit.

I'm gonna need to do this for floating point values as well >_>
I think I'll just take the simple approach and use the breakdown method for writing a float of the required byte-size.
anyways... that's out of the question... we're talking about signed ints... not floats

there's another idea I had about ranging the value to shrink the binary into the new format...
(Nintendo does this with int vertices and applies an exponent to a two's complemented equation using the int (a pseudo-float))
maybe I'll just use an external library to managge that... heh

my original idea for dealing with overflow was to max -250 between -128 and 127 for s8 ints. (result would be -128)

Tcll

Posts: 107
Joined: Wed Jan 01, 2014 6:36 pm

Previous

Return to General Coding Help

### Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests