# Artem's blog

Mainly .NET (C#, ASP.NET) and my projects

# Investigating mod

Almost all languages have operators. Example of these are, “+” – addition, “-” – subtraction, “*” – multiplication, “/” division. There are also more advanced operators such as “Mod”, “Exponent”, and “And”. Let us investigate the mod.

I have seen that there are much misunderstanding about mod. Some claims that it performs a modulo operation, some claims that it is the reminder. Actually it is a reminder, and not a modulo operation.

In this investigation, I chosen Visual Basic as the default language. So, let us try to use mod operation as a modulo operation.

```'default mod, "reminder"
MsgBox(3 - 4 Mod 6) ' result is -1
MsgBox(-1 + 4 Mod 6) ' result is 3```

I would like to add that there is no problem to get the same answer back, i.e., 3 -4 mod 6 is -1, and then by adding 4 and again mod, will result 3. However, that is not the modulo. To display the modulo, use the function below:

```   Function mod_r(ByVal x As Integer, ByVal y As Integer) As Integer
'the "real" modulo operation
Return x - y * Int(x / y)
End Function```

In arrays, tables, indexes, where the index should be positive, we can use the function above.

```        MsgBox(mod_r(3 - 4, 6)) ' result is 5
MsgBox(mod_r(5 + 4, 6)) ' result is 3```

# Sample of Bubble sort

The MsgBox is there to show how it actually goes through the integer array. You can remove it as well!

```    ' A sample of Bubble sort
' Have fun!

Dim i() As Integer = New Integer(4) {2, 5, 3, 1, 4}

Dim t As Boolean = True
While (t)
t = False
For j As Integer = 0 To 3

If i(j) > i(j + 1) Then

Dim _vec As Integer = i(j)
i(j) = i(j + 1)
i(j + 1) = _vec
t = True

'this is only to check the value of i
Dim _result As String = ""
For y As Integer = 0 To 4
_result &= i(y) & ","
Next
MsgBox(_result)
End If
Next
End While```

# CRC32 Method [VB.NET]

Please take a look over the code I’ve founded atÂ http://dotnet-snippets.com/dns/calculate-crc32-hash-from-file-SID587.aspx.
The author of this code is Tim Hartwig. If you want to read more about the code/algorithm, please go here!

```' This code have to be included in the header of file
Imports System.IO```
```Public Function GetCRC32(ByVal sFileName As String) As String
Try
Dim FS As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim CRC32Result As Integer = &HFFFFFFFF
Dim Buffer(4096) As Byte
Dim ReadSize As Integer = 4096
Dim Count As Integer = FS.Read(Buffer, 0, ReadSize)
Dim CRC32Table(256) As Integer
Dim DWPolynomial As Integer = &HEDB88320
Dim DWCRC As Integer
Dim i As Integer, j As Integer, n As Integer

'Create CRC32 Table
For i = 0 To 255
DWCRC = i
For j = 8 To 1 Step -1
If (DWCRC And 1) Then
DWCRC = ((DWCRC And &HFFFFFFFE)  2&) And &H7FFFFFFF
DWCRC = DWCRC Xor DWPolynomial
Else
DWCRC = ((DWCRC And &HFFFFFFFE)  2&) And &H7FFFFFFF
End If
Next j
CRC32Table(i) = DWCRC
Next i

'Calcualting CRC32 Hash
Do While (Count > 0)
For i = 0 To Count - 1
n = (CRC32Result And &HFF) Xor Buffer(i)
CRC32Result = ((CRC32Result And &HFFFFFF00)  &H100) And &HFFFFFF
CRC32Result = CRC32Result Xor CRC32Table(n)
Next i
Count = FS.Read(Buffer, 0, ReadSize)
Loop
Return Hex(Not (CRC32Result))
Catch ex As Exception
Return ""
End Try
End Function```