473,796 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do they cast it twice?

Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)

long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;

Is there any profound reason why they "Double" casting the _Val?
Thanks.
Mar 30 '08 #1
8 5497
On 30 mar, 10:06, ManicQin <Manic...@gmail .comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Not knowing the type of _Val, I can't say, but in general,
casting through an unsigned type guarantees a positive value in
the long (unless the positive value won't fit).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 30 '08 #2
ManicQin wrote:
Hi, I've browsed the STL code a bit and stumble upon the next line
(in the operator << overload of both long and short outputs - the
line is for checking does the input is a manipulator)

long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;

Is there any profound reason why they "Double" casting the _Val?
Thanks.
Yes, the idea is to get the value unsigned, to avoid sign extension
for hex and octal representation.
Bo Persson
Mar 30 '08 #3
>Not knowing the type of _Val, I can't say, but in general,
>casting through an unsigned type guarantees a positive value in
the long (unless the positive value won't fit).
The same behavior returns when _Val is long and short.
Yes, the idea is to get the value unsigned, to avoid sign extension
for hex and octal representation.
So why not just cast it directly to unsigned long... why the DOUBLE
cast...
I'm referring to the line (long)(unsigned short)_Val
besides ... when casting to unsigned and then to signed isn't the
result will be signed again?

(AFAIK long is synonymous with signed long int)
Mar 30 '08 #4
ManicQin wrote:
>Not knowing the type of _Val, I can't say, but in general,
casting through an unsigned type guarantees a positive value in
the long (unless the positive value won't fit).

The same behavior returns when _Val is long and short.
>Yes, the idea is to get the value unsigned, to avoid sign extension
for hex and octal representation.

So why not just cast it directly to unsigned long... why the DOUBLE
cast...
I'm referring to the line (long)(unsigned short)_Val
besides ... when casting to unsigned and then to signed isn't the
result will be signed again?
It will not be the same if the short value is negative.
Bo Persson

Mar 30 '08 #5
ManicQin <Ma******@gmail .comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)

long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;

Is there any profound reason why they "Double" casting the _Val?
Thanks.
Let's pretend that _Val is a short and sizeof(short) is 2 and
sizeof(long) is 4...

If _Bfl == oct or hex, and _Val == 0xFFFF (-1), then _Val will be cast
to (unsigned short)0xFFFF (65535) and then to (long)0xFFFF (65535).

whereas if _Bfl is not oct or hex and _Val == 0xFFFF (-1), then _Val
will be cast to (long)0xFFFFFFF F (-1).

As you can see, the double cast is important if you want to maintain the
literal bit pattern, but the single cast is important if you want to
maintain the logical decimal value.
Mar 30 '08 #6
On Mar 30, 7:06*pm, "Daniel T." <danie...@earth link.netwrote:
ManicQin <Manic...@gmail .comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
* * * * * * * * * * * * * * * * ? (long)(unsigned short)_Val //<-This Line
* * * * * * * * * * * * * * * * *: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks.

Let's pretend that _Val is a short and sizeof(short) is 2 and
sizeof(long) is 4...

If _Bfl == oct or hex, and _Val == 0xFFFF (-1), then _Val will be cast
to (unsigned short)0xFFFF (65535) and then to (long)0xFFFF (65535).

whereas if _Bfl is not oct or hex and _Val == 0xFFFF (-1), then _Val
will be cast to (long)0xFFFFFFF F (-1).

As you can see, the double cast is important if you want to maintain the
literal bit pattern, but the single cast is important if you want to
maintain the logical decimal value.
mmm... and that's why the else statement ":" returns just long to
maintain the value...
Ok thanks!
Mar 31 '08 #7
On Mar 31, 8:53 am, ManicQin <Manic...@gmail .comwrote:
On Mar 30, 7:06 pm, "Daniel T." <danie...@earth link.netwrote:
ManicQin <Manic...@gmail .comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks.
Let's pretend that _Val is a short and sizeof(short) is 2 and
sizeof(long) is 4...
If _Bfl == oct or hex, and _Val == 0xFFFF (-1), then _Val will becast
to (unsigned short)0xFFFF (65535) and then to (long)0xFFFF (65535).
whereas if _Bfl is not oct or hex and _Val == 0xFFFF (-1), then _Val
will becastto (long)0xFFFFFFF F (-1).
As you can see, the doublecastis important if you want to maintain the
literal bit pattern, but the singlecastis important if you want to
maintain the logical decimal value.

mmm... and that's why the else statement ":" returns just long to
maintain the value...
Ok thanks!
Just wondering, is there an obvious reason why STL doesn't use the C++
cast operators? In this case I believe the appropriate one would be
static_cast<>.

T
Mar 31 '08 #8
On 31 Mar, 22:50, tommy.hi...@gma il.com wrote:
[snip]
Just wondering, is there an obvious reason why STL doesn't use the C++
cast operators? In this case I believe the appropriate one would be
static_cast<>.
I'd just like to point out that the example you refer to is from an
iostreams implementation, which is not part of the STL. It could be
that this code was written before the "new" cast operators became
widely used, or maybe this particular implementor prefers the C-style
cast.

I could be wrong, but I do believe the original STL implementation
predates the new cast operators. However, there are certainly STL
implementations that uses them. (SGI, for example).

DP
Apr 1 '08 #9

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

Similar topics

0
3595
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: -- These two have the same output, other than the width: select dbo.ufn_vbintohexstr(0x123456789abcdef1234) select 0x123456789abcdef1234
15
4899
by: Michael Baehr | last post by:
I recently upgraded my Arch Linux system to GCC 3.4, and found out that a previously accepted behavior (cast-as-lvalue) is now marked as deprecated, and will cease to function in GCC 3.5. This has caused several builds to break, most notably elfutils. Presumably this behavior is not part of the C standard and it is thus being excised like many other nonstandard GCC extensions. Regardless, my question is not about the specifics or...
182
7561
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
8
2766
by: TS | last post by:
Hi, i have inherited a page from another user. The page is the target frame in a frameset. for some reason the whole page runs twice (page_load, init, etc. all run twice) I can't figure out how this is happening. there are no transfers or redirects that are occurring in the pages. the page runs thru all of its events then starts all over again. Trying to inspect the stack track doesn't help because when i get to the init the 2nd time,...
14
2556
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if both statements are similar : *((char **)v)++ == *((char **)v++) Where v is a pointer to an array of characters,defined as char *v;
3
6469
by: fniles | last post by:
I am accessing MS Access db with tblA whose column UNABLE can have NULL value. When I access UNABLE whose value is null, I got an error " Run-time exception thrown : System.InvalidCastException - Specified cast is not valid." either when I do IsDBNull(dr.GetString(0)) or dr.GetString(0) How can I fix this problem ? Thanks.
6
527
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a direct way to perform this cast? If not, is there way in C++ to do it? For example, I would like my function test() to work the way it is coded below. It produces a compiler error though: #include <pshpack2.h// 16-bit padding
17
2518
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
3
10669
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience with this and if there is a better solution. -jeff
0
9683
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
9529
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
10457
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
10013
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
9054
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
7550
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
6792
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();...
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.