473,408 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Assigning unsigned long to unsigned long long

The essence of a multiply-with-carry RNG is to have
declarations in the RNG proc, such as

unsigned long mwc( ){
static unsigned long x = 123456789, c = 362436;
unsigned long long t, a = 916905990LL;
\*
then reatedly form the 64-bit t = a*x+c, after which
the new carry c is the top 32,
and the new x (output), is the bottom 32 bits of t:
*/
t = a*x+c; c = t>>32; return x=t ;
}

Some compilers may give a warning for
assigning an unsigned long to an unsigned long long.
If so, would casting,
x=(unsigned long),
or chopping,
x=t&0xffffffff;
supress such warnings?
Would some compilers balk at the assignment
x=t; ?

Warnings aside, in machines for which
unsigned longs are 32 bits and
unsigned long longs are 64,
would the three versions
x = t;
x = (unsigned long) t;
x = t&0xffffffff;
all produce the required result: the bottom 32-bits of t?
Is the assembler code likely to differ?

I use gcc via djgpp in DOS, and x = t; works as expected,
with no warnings when compiled with the -Wall option.

George Marsaglia

Nov 13 '05 #1
1 6885
George Marsaglia wrote:

The essence of a multiply-with-carry RNG is to have
declarations in the RNG proc, such as

unsigned long mwc( ){
static unsigned long x = 123456789, c = 362436;
unsigned long long t, a = 916905990LL;
\*
then reatedly form the 64-bit t = a*x+c, after which
the new carry c is the top 32,
and the new x (output), is the bottom 32 bits of t:
*/
t = a*x+c; c = t>>32; return x=t ;
}

Some compilers may give a warning for
assigning an unsigned long to an unsigned long long.
If so, would casting,
x=(unsigned long),
or chopping,
x=t&0xffffffff;
supress such warnings?
Compilers can issue whatever diagnostics they feel
like: "suspicious" conversions, comparisons of unsigned
integers to negative numbers, or spellnig errors in your
comments. There is no particular language construct that
is guaranteed to shut up all compilers all the time.

That said, the cast is more likely than the mask to
quiet a compiler's squeaky wheels. The masking operation
produces the result you desire, but the type will still
be u.l.l. -- and a compiler that likes to complain about
type demotions is still likely to do so, not realizing
that in the case at hand no damage can occur. (True story:
a certain compiler once complained about `float f = 0.0;'
on the grounds that demotion from `double' to `float' might
lose precision; I had to rewrite as `float f = 0.0f;' to
get it to shut its stupid mouth ...). Of course, even with
the cast the compiler may still bitch about the phase of
the moon or something.
Would some compilers balk at the assignment
x=t; ?
Some might. They've got to accept and execute the code
(absent any other problems), but they're free to whinge.
A cast may or may not shut 'em up.
Warnings aside, in machines for which
unsigned longs are 32 bits and
unsigned long longs are 64,
would the three versions
x = t;
x = (unsigned long) t;
x = t&0xffffffff;
all produce the required result: the bottom 32-bits of t?
Yes. The first two differ only in that the conversion
is implicit in the first and explicit in the second; the
exact same conversion occurs anyhow. The third develops
the value in u.l.l. form and then demotes it to u.l. without
further change. All three assign the same value to `x'.
Is the assembler code likely to differ?
Yes. No. Maybe. And that's definite! At a guess, the
mask version might be different from the other two -- but
it's even in the realm of possibility that three entirely
different instruction sequences might be generated.
I use gcc via djgpp in DOS, and x = t; works as expected,
with no warnings when compiled with the -Wall option.


<off-topic>

I'd suggest using -W as well as -Wall -- I don't know
off-hand whether that would elicit additional diagnostics
for the case you mention, but it's occasionally saved me
from my own blunders. I've always been fond of encouraging
the compilers/linkers/assemblers/whatever to give me all
the constructive criticism they're capable of ...

</off-topic>

--
Er*********@sun.com
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ben | last post by:
Hi all, I would like to know if there is an easy way to assign a string to an int. I have a struct such as: struct Values { int a; int b; }
7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
9
by: Noel Milton | last post by:
Hi: Ok, I've just read in up to 65,535 bytes into a char array (using the recvfrom socket API call). So I have an array of 8 bit char's (char recvString;). Now, four (4) consecutive bytes...
31
by: leonm54 | last post by:
I remember that this is a bad practice, but can not find a definitive resource that states it is bad and why. Can anyone help?
14
by: moumita | last post by:
Hi All, I need to convert 4 bytes to an unsigned long. Suppose I have one array like unsigned char buf.I need to convert these 4 bytes into a single unsigned long. Is the following piece of code...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
9
by: shortyzms | last post by:
I'm having a problem with assigning 64-bit hex values to unsigned long variables in MS VS2005 c++ compiler. unsigned long Number; Number = 0x1000000000000000UL; After this declaration and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.