Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
SuggestionRe: Create Dynamic buttons Pin
Richard Deeming8-May-17 1:54
mveRichard Deeming8-May-17 1:54 
QuestionHow to Do Implementation of this questions Please Pin
Member 131757266-May-17 11:08
Member 131757266-May-17 11:08 
AnswerRe: How to Do Implementation of this questions Please Pin
Mycroft Holmes6-May-17 14:00
professionalMycroft Holmes6-May-17 14:00 
AnswerRe: How to Do Implementation of this questions Please Pin
Pete O'Hanlon6-May-17 22:00
mvePete O'Hanlon6-May-17 22:00 
Questionanother way Type 'short is short-shrifted, but not short-shifted Pin
BillWoodruff5-May-17 1:10
professionalBillWoodruff5-May-17 1:10 
AnswerRe: another way Type 'short is short-shrifted, but not short-shifted Pin
Jochen Arndt5-May-17 1:32
professionalJochen Arndt5-May-17 1:32 
AnswerRe: another way Type 'short is short-shrifted, but not short-shifted Pin
Pete O'Hanlon5-May-17 1:35
mvePete O'Hanlon5-May-17 1:35 
AnswerRe: another way Type 'short is short-shrifted, but not short-shifted Pin
harold aptroot5-May-17 3:05
harold aptroot5-May-17 3:05 

I conclude bit-shifts cast a 'short to an 'int"


That is true. It also applies to all other arithmetic and bitwise operators. It happens because there are no pre-defined operators for them, for example the list of pre-defined left shift operators is, according to ECMA 364:
Quote:
int operator <<(int x, int count);
uint operator <<(uint x, int count);
long operator <<(long x, int count);
ulong operator <<(ulong x, int count);

(note also that all shift counts are int)

Since an implicit conversion exists from short to int, you can still write someShort << k and it will pick the first overload and insert an implicit conversion.

As for why, I suspect this is a case of "because C does it and there was not enough reason to change it". Personally I don't like it, I would have preferred the extra overloads to exist, I find that most of the time when I work with a narrow type it is either widened immediately upon loading it (which without this implicit conversion could still use the implicit conversion that happens when assigning it to a temporary variable) or it has to stay narrow forever (which would become a lot less messy by changing it from C-style to "all the overloads").

A commonly cited non-reason is that, supposedly, "processors are optimized for their word size". But they aren't really, and "word size" is a badly defined can of worms to begin with. Even for "problematic cases" such as 16bit arithmetic on x86, x64, MIPS and a bunch of others, it would be completely trivial for the compiler to do the math in 32bit as much as possible and only insert the bare minimum of operations required to keep the range down to 16bits. Most operations don't propagate information from the msb down to the lsb, so you can work with "crap in the higher bits" without it affecting anything until one of the exceptions to the rule is encountered (division, right shift, comparison, function call).

As a bonus for "future work", keeping calculations narrow would make it easier to auto-vectorize effectively. The amount of parallelism is SIMDwidth / elementsize so the element size should be as small as possible. Not that the .NET JIT compiler does much auto-vectorization, but it could, and with the auto-widening semantics as they are it takes extra analysis to narrow everything back down again and keep the parallelism up. Even ahead-of-time compilers are still pretty bad at that, often taking the simple route of vectorizing the code extremely literally, including all useless widening and subsequent narrowing.

modified 5-May-17 9:16am.

GeneralRe: another way Type 'short is short-shrifted, but not short-shifted Pin
BillWoodruff5-May-17 4:54
professionalBillWoodruff5-May-17 4:54 
GeneralRe: another way Type 'short is short-shrifted, but not short-shifted Pin
harold aptroot5-May-17 5:19
harold aptroot5-May-17 5:19 
GeneralRe: another way Type 'short is short-shrifted, but not short-shifted Pin
BillWoodruff5-May-17 7:44
professionalBillWoodruff5-May-17 7:44 
QuestionMessage Closed Pin
2-May-17 6:44
SHALCHAUHAN2-May-17 6:44 
AnswerRe: Regarding Website Pin
NotPolitcallyCorrect2-May-17 6:54
NotPolitcallyCorrect2-May-17 6:54 
AnswerRe: Regarding Website Pin
User 41802543-May-17 2:56
User 41802543-May-17 2:56 
QuestionLink DLL at runtime Pin
icristut2-May-17 1:36
icristut2-May-17 1:36 
AnswerRe: Link DLL at runtime Pin
Richard MacCutchan2-May-17 2:22
mveRichard MacCutchan2-May-17 2:22 
GeneralRe: Link DLL at runtime Pin
icristut2-May-17 2:29
icristut2-May-17 2:29 
GeneralRe: Link DLL at runtime Pin
Richard MacCutchan2-May-17 2:35
mveRichard MacCutchan2-May-17 2:35 
GeneralRe: Link DLL at runtime Pin
icristut2-May-17 4:29
icristut2-May-17 4:29 
GeneralRe: Link DLL at runtime Pin
Pete O'Hanlon2-May-17 4:38
mvePete O'Hanlon2-May-17 4:38 
GeneralRe: Link DLL at runtime Pin
icristut2-May-17 4:44
icristut2-May-17 4:44 
GeneralRe: Link DLL at runtime Pin
Dave Kreskowiak2-May-17 4:16
mveDave Kreskowiak2-May-17 4:16 
AnswerRe: Link DLL at runtime Pin
ZurdoDev2-May-17 4:38
professionalZurdoDev2-May-17 4:38 
GeneralRe: Link DLL at runtime Pin
icristut2-May-17 4:41
icristut2-May-17 4:41 
QuestionForm Designer Pin
Mangesh Sachane2-May-17 1:15
Mangesh Sachane2-May-17 1:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.