Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading a code and unable to understand ~i in code
Here's is the code

What I have tried:

C++
<pre>class Solution {
public:
    int romanToInt(string S) {
        int ans = 0, num = 0;
        for (int i = S.size()-1; ~i; i--) {
            switch(S[i]) {
                case 'I': num = 1; break;
                case 'V': num = 5; break;
                case 'X': num = 10; break;
                case 'L': num = 50; break;
                case 'C': num = 100; break;
                case 'D': num = 500; break;
                case 'M': num = 1000; break;
            }
            if (4 * num < ans) ans -= num;
            else ans += num;
        }
        return ans;        
    }
};
Posted
Updated 28-Jun-22 11:40am
Comments
jeron1 28-Jun-22 12:09pm    
~ is the One's complement operator, (inverts all the bits).

https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
KarstenK 29-Jun-22 3:22am    
at most: it is bad style becaus hardly to undestand and easily an issue for compiler bugs and changes. So avoid it better.

Quote:
I am reading a code and unable to understand ~i in code

In this case it simply means i>=0.
 
Share this answer
 
Comments
Greg Utas 28-Jun-22 18:38pm    
I think it actually means i != 0, which will have the same effect. What amazes me is that some C and C++ programmers think it's brilliant to save a few keystrokes, so they write crap like this.
jeron1 28-Jun-22 20:08pm    
Greg Utas wrote: "What amazes me is that some C and C++ programmers think it's brilliant to save a few keystrokes, so they write crap like this."

:thumbs up: Indeed.
merano99 28-Jun-22 20:25pm    
Unfortunately, the result at i != 0 is wrong and thus does not have the same effect at all.
If you look at the generated code, there is no question of saving, on the contrary.

A look at the generated assembler code shows that the creative coding rather means more effort and is also more difficult to understand for humans.
for (int i = S.size() - 1; i >= 0; i--) {
 
00A17B45 83 E8 01             sub         eax,1  
00A17B48 89 45 D4             mov         dword ptr [ebp-2Ch],eax  
00A17B4B 83 7D D4 00          cmp         dword ptr [ebp-2Ch],0  
00A17B4F 0F 8C A2 00 00 00    jl          $LN13+2Bh (0A17BF7h)

for (int i = S.size() - 1; ~i; i--) {

00A07B45 83 E8 01             sub         eax,1  
00A07B48 89 45 D4             mov         dword ptr [ebp-2Ch],eax  
00A07B4B 8B 45 D4             mov         eax,dword ptr [ebp-2Ch]  
00A07B4E F7 D0                not         eax  
00A07B50 85 C0                test        eax,eax  
00A07B52 0F 84 A2 00 00 00    je          $LN13+2Bh (0A07BFAh)

(not optimized code, as you can see)
Greg Utas 28-Jun-22 23:19pm    
You're right. It's ~(-1), not ~0, that will be interpreted as false.
Invert each bit in the number: C++ Bitwise Operators[^]

A very simple google would have found that for you in seconds: in future you will save yourself a considerable amount of time if you do basic research first ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900