473,651 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with an array problem.

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 1644
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

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
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
ja*****@gmail.c om 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

Robert Kern wrote:
ja*****@gmail.c om 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
SpreadTooThin wrote:
Robert Kern wrote:
>ja*****@gmail.c om 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?
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.uin t32)

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

SpreadTooThin wrote:
Robert Kern wrote:
ja*****@gmail.c om 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(numb er) 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(numb er) casting"
each example to be.

Oct 3 '06 #8
Dennis Lee Bieber <wl*****@ix.net com.comwrote:
> 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 <stdio.h>
#include <string.h>

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3046
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
9
2116
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
2
2004
by: JackM | last post by:
Let me attempt to explain my problem. I have a crude php script that takes a text list of songs that was generated by an mp3 list program and translates each entry into the form where they can be inserted into my mySQL database in the proper fields although it is currently being written to another text file because of the problem I have below. The lines from the mp3 text file will look like this: Al Green - The Supreme Al Green - 01 -...
4
5966
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have been reading this ng all semester, and found it very helpful. I just have a stupid problem going on right now. Here's the project: We have to make a little frame with a text field, where the user inputs a number, and a text area where a...
7
6488
by: lkrubner | last post by:
The PHP scripting language has the array_unique() function that gets the unique, non-redundant values out of an array. Does Javascript have anything similar?
31
2626
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant but it appears to work. using time, i measured it takes .041s to execute, which i admit isnt much. but, the problem is that this procedure will be called many, many times in my project (probably at least a few thousand times, if not more) so...
2
3355
by: Thomas Connolly | last post by:
Anyone know if there is a C# equivallent to: enum { LIFFE_SIZE_AUTOMARKETREF = 15 }; typedef char LiffeAutoMarketReference ; Thanks,
23
2530
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
8
2736
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only the sequence nos and it should be such that i can access it through the structure .plz help me .
12
3872
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.