Am 05.07.2011 00:26, schrieb Nicolas Cellier:
2011/7/5 Douglas McPherson <djm1329@san.rr.com>:
Two's complement is a platform representation of integers not (necessarily) a smalltalk one. The concept of negative numbers has nothing to do with the radix a number is expressed in.
There are convenient extensions to the ByteArray protocol added by FFI which allow easy conversion though. I don't have time this second to list them, but will do so in a few hours.
That's true, but the internal implementation leaks out the bitAnd: bitOr: and other bit operations...
Nicolas
On Jul 4, 2011, at 15:14 , Stéphane Ducasse wrote:
Sven do your code work with Smalltalk smallInteger? Because I think that in that case you should have 31 bits?
I would like to see the two complement representation of numbers. This is what I do, for reading/writing unsigned or two complement signed integer from/to byte streams.
Note that two complement is only defined for a specific number size, 8, 16, 32 bits.
unsignedToSigned: integer size: size ^ integer < (2 raisedTo: size - 1) ifTrue: [ integer ] ifFalse: [ (self twoComplement: integer size: size) negated ]
signedToUnsigned: integer size: size ^ integer negative ifTrue: [ self twoComplement: integer size: size ] ifFalse: [ integer ]
twoComplement: integer size: size | mask | mask := (2 raisedTo: size) - 1. ^ mask bitAnd: ((integer abs bitXor: mask) + 1)
These are also very handy in this context (I believe I once submitted that as an issue):
integerFromByteArray: bytes | integer | integer := 0. bytes withIndexDo: [ :each :index | integer := integer + (each bitShift: (bytes size - index) * 8) ]. ^ integer
and Integer>>#asByteArrayOfSize:
Once you have a byte representation, you can render it as bits as well.
Sven
Sorry for breaking into the thread.
A very good hint to get the picture of Integer representation in the twos complement is the "Zahlenkreis". I always use the "Zahlenkreis" if I have to explain someone the twos complement representation of fixed point numbers. See for instance ti.uni-due.de/vs/de/education/Dv1/vorlesung2006/Folie-Zahlenkreis.pdf for a transparency. (That given link is not a special choice for any reason and I will not be paied for - you find many links with the search topic "Zahlenkreis".) I found a french translation: représentation circulaire. I think it is called fixed-point circle in english: http://www.mathworks.com/products/fixed/demos.html?file=/products/demos/ship... The Large Integers obviously are represented symmetric with a leading sign bit. (SmallInteger maxVal +1) printStringBase: 2 '1000000000000000000000000000000' It is astonisching that the smallest LargePositiveInteger has 30 0`s? (SmallInteger maxVal +2) printStringBase: 2 '1000000000000000000000000000001' (SmallInteger maxVal +1) negated printStringBase: 2 '-1000000000000000000000000000000' (SmallInteger maxVal +2) negated printStringBase: 2 '1000000000000000000000000000001' (SmallInteger maxVal raisedTo: 10) printStringBase: 2 '111111111111111111111111110110000000000000000000000000101100111111111111111111111110001000000000000000000000000011010001111111111111111111111100000100000000000000000000000011010001111111111111111111111110001000000000000000000000000000101100111111111111111111111111110110000000000000000000000000000001' (SmallInteger maxVal raisedTo: 10) negated printStringBase: 2 '-111111111111111111111111110110000000000000000000000000101100111111111111111111111110001000000000000000000000000011010001111111111111111111111100000100000000000000000000000011010001111111111111111111111110001000000000000000000000000000101100111111111111111111111111110110000000000000000000000000000001' An interesting topic! Regards BB