473,761 Members | 8,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6908
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
5818
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
3607
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
16
5137
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
9
2382
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 somewhere within that array represent a counter. How can I take those 4 individual bytes, concatinate the 32bits that
31
17705
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
12323
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 is right??Or is it a right approch to do that?? unsigned long temp; temp= (unsigned long) buff; temp | =((unsigned long) buff) << 8;
7
5048
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 involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
6
6458
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 vaguely . Can
9
16059
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 assignment, the value in Number is always 0. However, if the hex value is 0x0000000000000001, then the value is 1 as expected. It seems like the compiler is truncating the most significant 32 bits. How can I make this work?
0
9376
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
10136
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
9988
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...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
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
7358
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
6640
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
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.