Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); functionality

System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); functionality

Scheduled Pinned Locked Moved C#
csharp
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Pierre besquent

    Hi, I get a program c# when googling that validates the IBAN :

    static bool isIban (string code_iban)
    {
    // 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
    code_iban = code_iban.ToUpper () ;
    StringBuilder sb = new StringBuilder ();
    foreach ( char car in code_iban)
    {
    if (char.IsDigit (car) || char.IsLetter (car))
    sb.Append (car);
    }
    code_iban = sb.ToString ();
    if (code_iban.Length < 15 || code_iban.Length > 34)
    return false;
    // 2. Déplacer les 4 premiers caractères à droite
    string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
    // 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
    StringBuilder sb2 = new StringBuilder ();
    foreach (char c in iban)
    {
    int entier;
    if (char.IsLetter(c))
    entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
    else
    entier = Convert.ToChar (c) - Convert.ToChar ("0");
    sb2.Append (entier);
    }
    string checkSumString = sb2.ToString();
    // 4. Diviser le nombre ainsi obtenu par 97.
    int checksum = int.Parse(checkSumString.Substring(0, 1));
    int i = 1;
    while (i < checkSumString.Length)
    {
    int v = int.Parse(checkSumString.Substring(i, 1));
    checksum *= 10;
    checksum += v;
    checksum = checksum % 97;
    System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
    }
    if (checksum == 1)
    return true;
    else
    return false;
    }
    }

    I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #2

    Well, Math.Max is retrieving the maximum of i or i-1. Interlocked.Increment is used to increment i and store it all as an atomic action.

    I'm not a stalker, I just know things. Oh by the way, you're out of milk.

    Forgive your enemies - it messes with their heads

    My blog | My articles | MoXAML PowerToys | Onyx

    P 1 Reply Last reply
    0
    • P Pete OHanlon

      Well, Math.Max is retrieving the maximum of i or i-1. Interlocked.Increment is used to increment i and store it all as an atomic action.

      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      P Offline
      P Offline
      Pierre besquent
      wrote on last edited by
      #3

      Hi, If u can give me the // algorithme of this.I can do this like this simply

      Math.Max( ref i , i-1)
      i ++;

      why using of Interlocked.Increment??????? ty

      P 1 Reply Last reply
      0
      • P Pierre besquent

        Hi, I get a program c# when googling that validates the IBAN :

        static bool isIban (string code_iban)
        {
        // 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
        code_iban = code_iban.ToUpper () ;
        StringBuilder sb = new StringBuilder ();
        foreach ( char car in code_iban)
        {
        if (char.IsDigit (car) || char.IsLetter (car))
        sb.Append (car);
        }
        code_iban = sb.ToString ();
        if (code_iban.Length < 15 || code_iban.Length > 34)
        return false;
        // 2. Déplacer les 4 premiers caractères à droite
        string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
        // 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
        StringBuilder sb2 = new StringBuilder ();
        foreach (char c in iban)
        {
        int entier;
        if (char.IsLetter(c))
        entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
        else
        entier = Convert.ToChar (c) - Convert.ToChar ("0");
        sb2.Append (entier);
        }
        string checkSumString = sb2.ToString();
        // 4. Diviser le nombre ainsi obtenu par 97.
        int checksum = int.Parse(checkSumString.Substring(0, 1));
        int i = 1;
        while (i < checkSumString.Length)
        {
        int v = int.Parse(checkSumString.Substring(i, 1));
        checksum *= 10;
        checksum += v;
        checksum = checksum % 97;
        System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
        }
        if (checksum == 1)
        return true;
        else
        return false;
        }
        }

        I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #4

        This line:

        System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);

        seems utterly stupid to me. Comparing a number which is increased to what it was before and taking the maximum will always return the increased number. Even so, nothing is done with the return value. And why is this increase done using the interlocked class? Doesn't look terribly multi-threaded to me and if it is then you need additional locking. i++ is probably what you want, and I would expect the loop initialisation to set i = 0, rather than i = 1 on casual inspection.

        Regards, Rob Philpott.

        1 Reply Last reply
        0
        • P Pierre besquent

          Hi, I get a program c# when googling that validates the IBAN :

          static bool isIban (string code_iban)
          {
          // 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
          code_iban = code_iban.ToUpper () ;
          StringBuilder sb = new StringBuilder ();
          foreach ( char car in code_iban)
          {
          if (char.IsDigit (car) || char.IsLetter (car))
          sb.Append (car);
          }
          code_iban = sb.ToString ();
          if (code_iban.Length < 15 || code_iban.Length > 34)
          return false;
          // 2. Déplacer les 4 premiers caractères à droite
          string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
          // 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
          StringBuilder sb2 = new StringBuilder ();
          foreach (char c in iban)
          {
          int entier;
          if (char.IsLetter(c))
          entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
          else
          entier = Convert.ToChar (c) - Convert.ToChar ("0");
          sb2.Append (entier);
          }
          string checkSumString = sb2.ToString();
          // 4. Diviser le nombre ainsi obtenu par 97.
          int checksum = int.Parse(checkSumString.Substring(0, 1));
          int i = 1;
          while (i < checkSumString.Length)
          {
          int v = int.Parse(checkSumString.Substring(i, 1));
          checksum *= 10;
          checksum += v;
          checksum = checksum % 97;
          System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
          }
          if (checksum == 1)
          return true;
          else
          return false;
          }
          }

          I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #5

          it doesn't make sense at all; i is a local variable, no other thread can touch it, so the whole statement boils down to i++ and the while loop should have been a for loop. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          1 Reply Last reply
          0
          • P Pierre besquent

            Hi, If u can give me the // algorithme of this.I can do this like this simply

            Math.Max( ref i , i-1)
            i ++;

            why using of Interlocked.Increment??????? ty

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #6

            You shouldn't. In this case, it's absolutely no use whatsoever - frankly, it looks like somebody is trying to be too clever for their own good. Interlocked increment only makes sense in multi-threaded scenarios where there is a possibility that somebody else may increment a shared variable. As i is local, it's pointless. This code looks like obfuscation through stupidity.

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            D 1 Reply Last reply
            0
            • P Pierre besquent

              Hi, I get a program c# when googling that validates the IBAN :

              static bool isIban (string code_iban)
              {
              // 1. Enlever les caractères indésirables (espaces, tirets, ...)=> Seuls les lettres majuscules et les chiffres sont autorisés
              code_iban = code_iban.ToUpper () ;
              StringBuilder sb = new StringBuilder ();
              foreach ( char car in code_iban)
              {
              if (char.IsDigit (car) || char.IsLetter (car))
              sb.Append (car);
              }
              code_iban = sb.ToString ();
              if (code_iban.Length < 15 || code_iban.Length > 34)
              return false;
              // 2. Déplacer les 4 premiers caractères à droite
              string iban = code_iban.Substring(4, code_iban.Length - 4) + code_iban.Substring(0, 4);
              // 3. Convertir les lettres en chiffres via une table de conversion (A=10, B=11, C=12 etc.)
              StringBuilder sb2 = new StringBuilder ();
              foreach (char c in iban)
              {
              int entier;
              if (char.IsLetter(c))
              entier = Convert.ToChar (c) - Convert.ToChar ("A") + 10;
              else
              entier = Convert.ToChar (c) - Convert.ToChar ("0");
              sb2.Append (entier);
              }
              string checkSumString = sb2.ToString();
              // 4. Diviser le nombre ainsi obtenu par 97.
              int checksum = int.Parse(checkSumString.Substring(0, 1));
              int i = 1;
              while (i < checkSumString.Length)
              {
              int v = int.Parse(checkSumString.Substring(i, 1));
              checksum *= 10;
              checksum += v;
              checksum = checksum % 97;
              System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1);
              }
              if (checksum == 1)
              return true;
              else
              return false;
              }
              }

              I understand all only the functionality of System.Math.Max(System.Threading.Interlocked.Increment(ref i), i - 1); ty

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #7

              You have already asked about this here[^]


              I know the language. I've read a book. - _Madmatt

              L 1 Reply Last reply
              0
              • N Not Active

                You have already asked about this here[^]


                I know the language. I've read a book. - _Madmatt

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                Yes, however this time there was a whole new sentence, and a big code snip, so we could provide more relevant comments. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                N 1 Reply Last reply
                0
                • L Luc Pattyn

                  Yes, however this time there was a whole new sentence, and a big code snip, so we could provide more relevant comments. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #9

                  And he is using C# not JavaScript :-D


                  I know the language. I've read a book. - _Madmatt

                  L 1 Reply Last reply
                  0
                  • N Not Active

                    And he is using C# not JavaScript :-D


                    I know the language. I've read a book. - _Madmatt

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #10

                    He is flexible for now, in the end he'll really want JavaScript code doing the same. Just you wait. :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      You shouldn't. In this case, it's absolutely no use whatsoever - frankly, it looks like somebody is trying to be too clever for their own good. Interlocked increment only makes sense in multi-threaded scenarios where there is a possibility that somebody else may increment a shared variable. As i is local, it's pointless. This code looks like obfuscation through stupidity.

                      I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Onyx

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #11

                      Pete O'Hanlon wrote:

                      obfuscation through stupidity

                      :laugh: 5

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups