simple question (signed ints)

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

simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 3:29 am

anyone know how to validate s8( -250 ) ??

s8 = signed 8-bit int

I just need an int value that's the correct bit-width for the data.
thanx :)

my attempts:
Code: Select all
value = -250
byte_size = 1

>>> abs(value)&((1<<((byte_size<<3)-1))-1)
122
>>> ~(abs(value)&((1<<((byte_size<<3)-1))-1))
-123
>>> pow(-1, value<0)*(abs(value)&((1<<((byte_size<<3)-1))-1))
-122

I assume -123 is the correct one?? >.>
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 10:50 am

What exactly are you trying to calculate here?
Without an explanation, your calculations make no sense.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Kebap » Mon Feb 03, 2014 12:30 pm

Signed 8 bit numbers can represent -128 to 127, (unsinged do 0 to 255), whereas -250 will not be possible to display inside this system. http://en.wikipedia.org/wiki/Integer_%28computer_science%29#Common_integral_data_types Hope it helps!
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 1:20 pm

nono... I'm trying to cut it...

lemme use an unsigned example...
256 & 255 = 0
1048 & 255 = 24

I expect the values that enter these classes ( bu32, bs8, s16, ... ) to be bigger than what the class can actually hold...
so in logic's terms, I'm making it fit by it's bit-width.

EDIT:
stranac wrote:What exactly are you trying to calculate here?
Without an explanation, your calculations make no sense.


I thought:
Tcll wrote:I just need an int value that's the correct bit-width for the data.

would've been good enough :/

guess not... heh
sorry


I'm not sure how to code it exactly, and need an example... :/

EDIT2:

an example that would take place with byte-magic on a file: (UMC script)
Code: Select all

value = bs16( -524 )

vptr = ref( value )
deref( vptr, bs8 ) # changes 'value' from a bs16 to a bs8 (changing the internal value and data in the memory)

User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 1:43 pm

So you want your ints to overflow?
If not, what is it that you want exactly?
How do you want to get "an int value that's the correct bit-width for the data."
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 1:55 pm

er... (how to explain?)

I'm trying to deal with the overflow by keeping it's bit-quality

fitting 258 (16-bit) into an 8-bit int would yeild 2.
0x0102

I know what to do for unsigned ints, but I'm not sure what to do for signed ints...
how do I calculate the value??

we get the sign by testing if value < 0
and then we abs( value ) to validate the bit-width...

wait a minute :o
Code: Select all
if value<0: value += 1; sign = True
abs( value ) & ((1<<((byte_size<<3)-1))-1)
if sign: value = (value*-1)-1

would that be right??
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 2:13 pm

Sorry, I still have no idea what you want to do.

Why would "fitting" 258 into 8 bits yield 2?
Do you just take the last 8 bits of the number?

Your calculations, once again, make no sense without knowing how you want to get the result.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Kebap » Mon Feb 03, 2014 2:28 pm

Tcll wrote:er... (how to explain?)

Maybe show a few examples, what is your input, what output do you want, how do you do it, how it would be wrong, etc.

Tcll wrote:how do I calculate the value??


Why you just use modulo:

Code: Select all
>> print 258 % 256
2
>>
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 2:44 pm

stranac wrote:Sorry, I still have no idea what you want to do.

dangit >_<

do you have skype??
we could talk this over on there...
'Tcll5850' is mine

stranac wrote:Why would "fitting" 258 into 8 bits yield 2?
Do you just take the last 8 bits of the number?


exactly...
how would it work in C++ pointers??

byte value = 258??

I'm trying to store it in "memory" by simply negating the overflow

EDIT:
Kebap wrote:
Tcll wrote:er... (how to explain?)

Why you just use modulo:

Code: Select all
>> print 258 % 256
2
>>


'3'
interesting, and gets rid of the extra (256)-1 for 258 & 255
(where 255 comes from (1<<(byte_size<<3))-1 )
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 2:54 pm

Kebap wrote:
Tcll wrote:er... (how to explain?)

Maybe show a few examples, what is your input, what output do you want, how do you do it, how it would be wrong, etc.


input:
Code: Select all
v = bs8( -250 )


output:
Code: Select all
print v.value >>> ?? (how would it store if we negated the overflow??)


EDIT: for the unsigned example (already working)
Code: Select all
v = bu8( 258 )

print v.value >>> 2
Last edited by Tcll on Mon Feb 03, 2014 2:57 pm, edited 2 times in total.
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 2:55 pm

So I'm guessing you do want to simulate integer overflow...

Simply taking the last 8 bits would not work, since you would end up with values larger than the max you can store.
Taking the last 7 bits wouldn't work, since you'd lose the sign information.

I would normally recommend just using ctypes for stuff like this, but I know you want to support your wonky types as well...
Something like this should work(tested only on bit_len of 8):
Code: Select all
def overflow(value, bit_len):
    max_num = (1 << (bit_len - 1)) - 1
    sign = value & (max_num + 1)
   
    new_value = value & max_num
    if sign:
        new_value -= max_num + 1
    return new_value


Edit: wait a second, I think I messed something up...
Edit2: corrected, was missing a -1
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 3:15 pm

your example:
Code: Select all
overflow( -250, 8 ) >>> 6

/was expecting a negative number

I think it's cause of your sign test...
me:
Code: Select all
sign = value<0


EDIT:
lemme test my example:
Tcll wrote:if value<0: value += 1; sign = True
abs( value ) & ((1<<((byte_size<<3)-1))-1)
if sign: value = (value*-1)-1
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 3:18 pm

I guess I have no idea what you want after all.

What would the result be for bs8(200), then? Not -56 I'm guessing...
You'll have to explain better what you want, because I still can't quite understand what it is.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 3:30 pm

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
Last edited by Tcll on Mon Feb 03, 2014 3:41 pm, edited 2 times in total.
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 3:33 pm

Tcll wrote:would what's in yellow be correct??

I. Do. Not. Know. What. You. Want.

If you just want them to behave as matching C types would, the code I posted works fine.
If you want help with doing something else, you will have to explain what it is you want.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 3:38 pm

I'm trying to preserve the 7 bits

look at my edit in my last post.
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 3:45 pm

Tcll wrote:I'm trying to preserve the 7 bits

Then there would be no difference between positive and negative numbers.
The only difference between the binary representation of the two is the highest bit, which represents the sign(or in python, just a minus sign).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 3:50 pm

stranac wrote:
Tcll wrote:I'm trying to preserve the 7 bits

Then there would be no difference between positive and negative numbers.
The only difference between the binary representation of the two is the highest bit, which represents the sign(or in python, just a minus sign).

if the bigger 15bit number is negative, then the resulting 7bit number should also be negative.
(that won't happen if you just take the 8th bit)

I get this from my experiences in working with the IEEE754 float format.
the highest-most bit should be carried over to the smaller number.
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 3:59 pm

Ok, I guess.
So take the result of abs(x) % 2**(n-1)(where x is the number you're converting, and n is the bit length), and if x was negative, make the result negative as well?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 4:15 pm

Code: Select all
def ov( v, bs ):
    return pow( -1, v<0 ) * ( abs(v)%2**((bs<<3)-1) )


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

>>> ov( 200, 1 )
72

>>> ov( -128, 1 )
0

>>> ov( 128, 1 )
0
>>> ov( 127, 1 )
127
>>> ov( -129, 1 )
-1
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Next

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests