443,994 Members | 1,166 Online
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

# Need help with an array problem.

 P: n/a Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', [65537]) b = array.array('H', [0]) b[0] = a[0] Which gives an overflow message.... So can't I truncate the long by discaring the upper bits .. Like b[0] = 0x0000FFFF & a[0] How does one normally cast an object from long to short? Oct 2 '06 #1
8 Replies

 P: n/a SpreadTooThin wrote: Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', [65537]) b = array.array('H', [0]) b[0] = a[0] Which gives an overflow message.... So can't I truncate the long by discaring the upper bits .. Like b[0] = 0x0000FFFF & a[0] How does one normally cast an object from long to short? Take the modulo 65536? pyarray.array('H', (array.array('L', [65537%65536]))) array('H', [1]) James -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/ Oct 2 '06 #2

 P: n/a SpreadTooThin wrote: Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', [65537]) b = array.array('H', [0]) b[0] = a[0] Which gives an overflow message.... As it should. So can't I truncate the long by discaring the upper bits .. Like b[0] = 0x0000FFFF & a[0] Any reason why you don't you just try it? | >>import array | >>a = array.array('L', [65537]) | >>b = array.array('H', [0]) | >>a[0] | 65537L | >>a[0] & 0xffff | 1L | >>b[0] = a[0] & 0xffff | >>b[0] | 1 | >>> Oct 2 '06 #3

 P: n/a To your question on casting long to short. This is how: a=1234L # long b=int(a) # int (short) John Machin skrev: SpreadTooThin wrote: Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', [65537]) b = array.array('H', [0]) b[0] = a[0] Which gives an overflow message.... As it should. So can't I truncate the long by discaring the upper bits .. Like b[0] = 0x0000FFFF & a[0] Any reason why you don't you just try it? | >>import array | >>a = array.array('L', [65537]) | >>b = array.array('H', [0]) | >>a[0] | 65537L | >>a[0] & 0xffff | 1L | >>b[0] = a[0] & 0xffff | >>b[0] | 1 | >>> Oct 2 '06 #4

 P: n/a ja*****@gmail.com wrote: To your question on casting long to short. This is how: a=1234L # long b=int(a) # int (short) No, a Python int is a C long. A Python long is an arbitrary-precision number and does not correspond to any C type. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco Oct 2 '06 #5

 P: n/a Robert Kern wrote: ja*****@gmail.com wrote: To your question on casting long to short. This is how: a=1234L # long b=int(a) # int (short) No, a Python int is a C long. A Python long is an arbitrary-precision number and does not correspond to any C type. -- So there is no short(number) casting? Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco Oct 3 '06 #6

 P: n/a SpreadTooThin wrote: Robert Kern wrote: >ja*****@gmail.com wrote: >>To your question on casting long to short. This is how:a=1234L # longb=int(a) # int (short) No, a Python int is a C long. A Python long is an arbitrary-precision number anddoes not correspond to any C type. So there is no short(number) casting? Not in core Python, no, since C short ints have no Python type directly corresponding to them. As John Machin pointed out, if you had tried what you proposed, it would have worked just fine. If you find that you keep needing to deal with the various C integer and floating point types (and arrays of such), you might want to consider using numpy. http://numpy.scipy.org In [15]: import numpy In [16]: a = numpy.array([65537], dtype=numpy.uint32) In [17]: a Out[17]: array([65537], dtype='uint32') In [18]: b = a.astype(numpy.uint16) In [19]: b Out[19]: array([1], dtype='uint16') In [20]: numpy.uint16(a[0]) Out[20]: 1 -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco Oct 3 '06 #7

 P: n/a SpreadTooThin wrote: Robert Kern wrote: ja*****@gmail.com wrote: To your question on casting long to short. This is how: a=1234L # long b=int(a) # int (short) No, a Python int is a C long. A Python long is an arbitrary-precision number and does not correspond to any C type. -- So there is no short(number) casting? It depends on what you think you mean by "short(number) casting". I thought I'd answered your original question -- if short = long & 0xffff doesn't do what you want to do, please give examples of what "long" means to you, and what you expect the result of "short(number) casting" each example to be. Oct 3 '06 #8

 P: n/a Dennis Lee Bieber Of course, there is also the confusion between "type cast" and "type conversion" -- at least, for me... cast taking the bit-pattern of a value in one "type" and interpreting the same bit-pattern as if it were a different "type" conversion taking the value of a bit-pattern in one "type" and converting it to the bit pattern of the equivalent value in another "type" From where have you learned those definitions? If it's from C, then you have read the wrong books. A cast in C is a type conversion, with the same semantics as you write under "conversion" above. The C standard (ISO/IEC 9899:1999) says: 6.5.4 Cast operators [...] Semantics Preceding an expression by a parenthesized type name converts the value of the expression to the named type. This construction is called a cast. A cast that specifies no conversion has no effect on the type or value of an expression. ("A cast that specifies no conversion" refers to when you cast from one type to the same type, e.g. '(int)x' if x is of the type 'int'.) You may also try this program: #include #include int main(void) { /* Assumption: sizeof(float)==sizeof(int). This is the most * common case on modern computers. */ float f = -17.0; int i = -23; float fjunk; int ijunk; printf("Cast: %d %10.6f\n", (int)f, (float)i); memcpy(&fjunk, &i, sizeof i); memcpy(&ijunk, &f, sizeof f); printf("Bitcopy: %d %10.6f\n", ijunk, fjunk); return 0; } If a cast had been what you believed, both printf() statements above would give the same output. Unless your C compiler uses some really strange floating point representation, they will print rather different things. The first one must print Cast: -17 -23.000000 showing very clearly that a cast is a type conversion. The second printf() will print some seemingly random numbers, showing that those bit-patterns will be interpreted very differently when interpreted as another type. What you might have been confused by, is dereferencing a casted pointer. Add the following statement: printf("Pointer cast: %d %10.6f\n", *(int*)&f, *(float*)&i); to the program. It should output the same numbers as the "Bitcopy" printf(). But what is cast here is the *address* of the variables, not the actual contents of them. It is the *dereferencing* of those casted pointers that interpret the bit patterns in the variables as if they were another type, not the casting itself. -- Thomas Bellman, Lysator Computer Club, Linköping University, Sweden "I don't think [that word] means what you ! bellman @ lysator.liu.se think it means." -- The Princess Bride ! Make Love -- Nicht Wahr! Oct 3 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.