473,320 Members | 1,713 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,320 software developers and data experts.

"(unsigned)" with long/int

Does the cast "(unsigned)" imply "(unsigned int)", or does it simply
strip the signedness from the variable?

In other words, given this:

long l = -123;
unsigned long ul = (unsigned)l;

Does the value of "l" get demoted to an unsigned int during the
assignment, or does it remain a long? (I am on a system where
sizeof int == sizeof long, so examining the generated code doesn't
help me out here.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Aug 7 '07 #1
10 3075
Kenneth Brody wrote:
>
Does the cast "(unsigned)" imply "(unsigned int)",
Yes.
or does it simply
strip the signedness from the variable?
No.
In other words, given this:

long l = -123;
unsigned long ul = (unsigned)l;

Does the value of "l" get demoted to an unsigned int during the
assignment or does it remain a long?
(I am on a system where
sizeof int == sizeof long, so examining the generated code doesn't
help me out here.)
The type and value of the assignment expression is:
((unsigned long)(unsigned)-123)

--
pete
Aug 7 '07 #2
Kenneth Brody <ke******@spamcop.netwrites:
Does the cast "(unsigned)" imply "(unsigned int)", or does it simply
strip the signedness from the variable?
[...]

In a cast or in any other context, 'unsigned' by itself is an
abbreviation for 'unsigned int'. Similarly, 'long', 'signed long',
'long int', and 'signed long int' are all equivalent.

See section 6.7.2 of the C99 standard (latest draft available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>, or the
corresponding section in any other version or draft of the C standard,
or any decent C textbook.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 7 '07 #3
pete <pf*****@mindspring.comwrites:
Kenneth Brody wrote:
[...]
>In other words, given this:

long l = -123;
unsigned long ul = (unsigned)l;

Does the value of "l" get demoted to an unsigned int during the
assignment or does it remain a long?
(I am on a system where
sizeof int == sizeof long, so examining the generated code doesn't
help me out here.)

The type and value of the assignment expression is:
((unsigned long)(unsigned)-123)
Actually, the expression '-123' is of type int; the operand of the
cast to 'unsigned' is 'l', which is of type long.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 7 '07 #4
Keith Thompson wrote:
>
pete <pf*****@mindspring.comwrites:
Kenneth Brody wrote:
[...]
In other words, given this:

long l = -123;
unsigned long ul = (unsigned)l;

Does the value of "l" get demoted to an unsigned int during the
assignment or does it remain a long?
(I am on a system where
sizeof int == sizeof long, so examining the generated code doesn't
help me out here.)
The type and value of the assignment expression is:
((unsigned long)(unsigned)-123)

Actually, the expression '-123' is of type int; the operand of the
cast to 'unsigned' is 'l', which is of type long.
Thank you.

--
pete
Aug 7 '07 #5
pete wrote:
>
Keith Thompson wrote:

pete <pf*****@mindspring.comwrites:
Kenneth Brody wrote:
[...]
>In other words, given this:
>>
> long l = -123;
> unsigned long ul = (unsigned)l;
>>
>Does the value of "l" get demoted to an unsigned int during the
>assignment or does it remain a long?
>(I am on a system where
>sizeof int == sizeof long, so examining the generated code doesn't
>help me out here.)
>
The type and value of the assignment expression is:
((unsigned long)(unsigned)-123)
Actually, the expression '-123' is of type int; the operand of the
cast to 'unsigned' is 'l', which is of type long.

Thank you.
However, the type and value of this expression:
((unsigned long)(unsigned)-123)
is the same as the type and value of:
((unsigned long)(unsigned)(long)-123)

--
pete
Aug 7 '07 #6
Keith Thompson wrote:
>
Kenneth Brody <ke******@spamcop.netwrites:
Does the cast "(unsigned)" imply "(unsigned int)", or does it simply
strip the signedness from the variable?
[...]

In a cast or in any other context, 'unsigned' by itself is an
abbreviation for 'unsigned int'. Similarly, 'long', 'signed long',
'long int', and 'signed long int' are all equivalent.
[...]

Thanks. That's what I was afraid of.

Drifting OT a little...

I'm just trying to figure out a way around a weirdness in a POSIX-like
library function. (Specifically, read() and write().) Not all
implementations are identical. (Some use int, some use size_t, for
example.)

In this case, the implementation takes an "unsigned int" for the
length, but returns "int" for the length actually read/written.
This means you can't use this without getting a warning:

if ( write(fd,buffer,len) < len )

Not a biggie, as there are ways around this.

Side question: is "ssize_t" (the signed counterpart to "size_t") a
standard type? I see no reference to it in n1124.pdf, so I assume
it's not. (Though it may be standard POSIX.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Aug 8 '07 #7
Kenneth Brody said:

<snip>
Side question: is "ssize_t" (the signed counterpart to "size_t") a
standard type?
No.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 8 '07 #8
"Kenneth Brody" <ke******@spamcop.netwrote in message
news:46***************@spamcop.net...
Side question: is "ssize_t" (the signed counterpart to "size_t") a
standard type? I see no reference to it in n1124.pdf, so I assume
it's not. (Though it may be standard POSIX.)
POSIX defines ssize_t, but ISO doesn't. It's functionally equivalent to
ptrdiff_t, though most people wouldn't think to use the latter for the same
purposes due to the name.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Aug 8 '07 #9
On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
>Side question: is "ssize_t" (the signed counterpart to "size_t") a
standard type? I see no reference to it in n1124.pdf, so I assume
it's not. (Though it may be standard POSIX.)
Standard (and required) POSIX type, not standard C.

--
Al Balmer
Sun City, AZ
Aug 8 '07 #10
On Wed, 08 Aug 2007 17:46:10 GMT, Al Balmer <al******@att.netwrote:
On Wed, 08 Aug 2007 11:42:50 -0400, Kenneth Brody
<ke******@spamcop.netwrote:
Side question: is "ssize_t" (the signed counterpart to "size_t") a
standard type? I see no reference to it in n1124.pdf, so I assume
it's not. (Though it may be standard POSIX.)

Standard (and required) POSIX type, not standard C.
<OTAnd used as the return type for POSIX read() and write(), and
send() and recv() etc., because they can return EITHER a count (>= 0
<= specified/desired len in call) OR -1 for error.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Aug 26 '07 #11

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

Similar topics

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...
43
by: M-One | last post by:
See subject: how do I calloc (and free the memory, if that's not free(my_bytes);) this? TIA!
2
by: jian | last post by:
Hi, I am looking at netinet/ip.h in linux code, and the declaration: unsigned int ip_hl:4 make me confused. I can understand "unsigned int ip_hl", but, what does ":4" here mean? Very...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
2
by: runner | last post by:
I'm trying to call some functions from OpenSSL library but I'm a bit confused when I have to use pinvoke. first function should create key from some input data, it's declared : void...
8
by: FabioAng | last post by:
Assuming I have this function (it's not complete): template<typename InputType, typename OutputIterator> void to_utf8(InputType input, OutputIterator result) { // trivial conversion if (input...
2
by: Pedrop | last post by:
I have to check if a long value is between two other long values (range check). Since all values are generated by shifting and because in java long is always signed the use of simple operators >= <=...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.