April 18, 2018 at 3:02 pm #14397
Why do you get these numbers ?Monkey12345678910111213141516171819202122232425262728293031Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Function Main()Print ByteWrap(56)Print ByteWrap(255) ' Why -1 and not 255?Print ByteWrap(256)Print ByteWrap(127)Print ByteWrap(128) ' Why -128 and not 0?Print ByteWrap(-1) ' Why -1 and not 255?Print "-----------"Print UByteWrap(56)Print UByteWrap(255)Print UByteWrap(256)Print UByteWrap(127)Print UByteWrap(128)Print UByteWrap(-1) ' Why 255 and not 0?EndFunction ByteWrap:Byte(value:Byte)Return (value & $FF)End FunctionFunction UByteWrap:UByte(value:UByte)Return (value & $FF)End FunctionApril 18, 2018 at 10:45 pm #14401
As opposed to what? The behavior is correct and reflects how casting to signed and unsigned data types works with all computer languages where a byte is defined as an 8 bit binary value.April 18, 2018 at 11:25 pm #14402
As apposed to what we wrote inside the source code.
How do you mean?
Okay, how do this work?April 18, 2018 at 11:45 pm #14403
Why is 255 & -1 = 0 instead of 255? -1 is all one bits except one, and we mask all bits but the 8 lowest bits. So all bits should be 1’s = 255.
The signed bytes below are harder to explain but if the above is explained that’s probably enough to understand these aswell.
The questions where (not stupid at all) these questions:
Why is 255 & 255 = -1 and not 255?
Why is 128 & 255 = -128 and not 0?
Why is -1 & 255 = -1 and not 255?
I know it’s about the interpretation of the actual values in edit time and runtime swell, but how are you supposed to reason about this?April 18, 2018 at 11:47 pm #14404
Any other demo coders in the building? Need help.April 19, 2018 at 12:00 am #14405
Ahh the value is seen as a 2nd complement AFTERWARDS, I’m stupid. Okay gotcha. Of course.
I just read BYTE and thought 0-255. 0-255 0 -255 0 -255 It’s signed.
But the unsigned one I haven’t got around yet to understand.April 19, 2018 at 12:01 am #14406
a signed bite only goes upto 128 and down to -128 which is the first 7 bits and are used as the number and the last bit “on” for negative and “off” for positive. if you pass a number greater than 128 it sets the last bit to on and therefore it returns a negative number and will be a completely different number that you passed as interpreted by the compiler.
in a signed byte a negative 1 in a byte binary equals 11111111 and in an unsigned byte the 11111111 is equal to 255. The last bit of a signed byte will always be used to represent whether the number is positive or negative. and that goes back all the way to the original assembly language.April 19, 2018 at 12:02 am #14407
no it goes up to 127April 19, 2018 at 12:03 am #14408
Yea my fault something like that. I believe it goes down to -128.April 19, 2018 at 12:08 am #14409
The unsigned is the only one left to explain and I think I understand it now too
-1 is interpreted all one bits so so it will give 255
So the thing that was fuzzy was that the functions where defined to get input and output of certain type.
And that type needs to be converted at the call of the function.
That conversion was the source of confusion. It needs time to get used to.April 19, 2018 at 12:10 am #14410
Is there a SAR in this language? Is there a way to do that? Maybe integer division? << >> >>>April 19, 2018 at 12:21 am #14411
I don’t know. Mark might have to answer that. I don’t think he has it integrated in the language and I don’t know if he would be willing to add it.
on another note, here is a little bit of code to kind of explain what I said before:Monkey123456789101112131415161718192021222324[crayon-5bc4a4c469ce9430314300 inline="true" ]Namespace myapp#Import "<std>"Using std..Function Main()Local a:UByte = 255Local b:Byte = -1Local c:String = ""For Local n:Int = 0 Until 8If (a & (1 Shl n)) c = "1"+c' shl = shift leftNextPrint "this is unsigned byte 255 in binary =" +cc = ""For Local n:Int = 0 Until 8If (b & (1 Shl n)) c = "1"+cNextPrint "this is singed byte -1 in binary = " + cPrint a+" " + bEnd
had the representation of the math backwards.April 19, 2018 at 12:42 am #14412
try this video if you have time:
April 19, 2018 at 12:51 am #14413
Thanks, ya it’s good to be clear about things.
Okay if it’s not implemented we can use something that does the same thing.
Something like x = Floor(x/2) ‘ Sar x,1 (but sometimes the correct Sar for a negative x would be -Ceil(-x / 2) when you use 2nd complement. It’s just an oddity to watch for out for.April 19, 2018 at 1:09 am #14414
Building an arcade emulator around a few processors and thinking of making it open-source if it turns out alright.
It’s all Monkey2 code and it tries to avoid externals, maybe there will be some externals we’ll see but it’s a fun project and very good practice.
It’s understandable if Mark doesn’t want to put low-level in there no worries but I would’ve guessed that there were an integer division that fulfilled the purpose of what Sar does. A hidden low-level element, a hackers kinder surprise.
You must be logged in to reply to this topic.