Reputation: 3496
Good morning, afternoon or night,
Forewarning: I know that this may sound like I am trying to do premature optimization, but I assure you I am not. That being, please answer this question if you would like to to the best of your ability without pointing me to links about "premature optimization".
Is it possible to notice any differences in the performance of massively repeated operations using constants written in base ten and in base sixteen? I see people writing
int b = a >> 0x1f;
a lot instead of
int b = a >> 31;
which suggests that either they come from a C/C++ background or that they may think operations perform faster (and I am not talking only about bitwise operations). Aren't all constants translated to the same base by the C# compiler? Or can there be any advantages in using base 16 over base 10 while writing code?
Thank you very much,
Upvotes: 1
Views: 475
Reputation: 33252
A number is a number, decimal or hex is just a representation to write the number on the screen in a way human can read. So there is no difference at all, neither in c/c++.
Upvotes: 0
Reputation: 142921
They are indeed interpreted as the exact same thing in the IL, and the first is harder to read for most people. So there's not much advantage to the hex method.
Upvotes: 0
Reputation: 50225
The only advantage I see is that bitwise operations typically make more sense in hex. The shift op, however, doesn't (in my opinion).
Upvotes: 0
Reputation: 4433
The only advantage is that it's more readable to someone who's used to working in hex. An integer is an integer, whether represented by a decimal literal, or a hex one.
Upvotes: 0
Reputation: 7621
Those two statements are identical to the compiler. It's just a matter of user preference.
Upvotes: 1
Reputation: 11477
It will make no difference as they will get compiled down to the same thing.
It does make the code more readable for binary operations as using hexadecimal makes it easier to read the bits. To convert from decimal -> binary is a lot harder to do mentally.
0x1f is going to be :
1 -> 0001
+
f -> 1111
= 00011111
Upvotes: 4