# How to check if a number is a power of 2 in C# and VB.NET

To check if a number is a power of 2 in C# and VB.NET you can use the following extension method.

## Sample C#

```public static bool IsPowerOfTwo(this ulong input)
{
return (input != 0) && ((input & (input - 1)) == 0);
}
```

## Sample VB.NET

```<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsPowerOfTwo(input As ULong) As Boolean
Return (input <> 0) AndAlso ((input And (input - 1)) = 0)
End Function
```

As always, the extension method was added to out Fesslersoft.Extensions library @Github

# How to calculate PercentOf in PHP

To calculate PercentOf in PHP you can use the following snippet. It will calculate the percent of 2 given integers.

## Sample PHP

```Function PercentOf(\$int1, \$int2) {
if (\$int2 <= 0 || \$int1 <=0){
return 0;
}
\$val1 = \$int1 / \$int2;
\$retVal = \$val1 * 100;
return \$retVal;
}
```

If there is a 0 passed it will return 0.

# PercentOf method for C# and VB.NET

This snippet will give you the PercentOf method for C# and VB.NET.

## Sample C#

```private decimal CalculatePercentOf(object value1, object value2)
{
decimal val1;
decimal val2;
decimal.TryParse(value1.ToString(), out val1);
decimal.TryParse(value2.ToString(), out val2);
decimal result = 0;
if (val1 > 0 && val2 > 0)
{
result = (decimal) val1/val2*100;
}
return result;
}

public static decimal PercentOf(sbyte first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(sbyte first, float second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(short first, float second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(int first, float second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(long first, float second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(double first, float second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(decimal first, float second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, sbyte second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, short second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, int second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, long second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, double second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, decimal second)
{
return CalculatePercentOf(first, second);
}

public static decimal PercentOf(float first, float second)
{
return CalculatePercentOf(first, second);
}
```

## Sample VB.NET

```Private Function CalculatePercentOf(value1 As Object, value2 As Object) As Decimal
Dim val1 As Decimal
Dim val2 As Decimal
Decimal.TryParse(value1.ToString(), val1)
Decimal.TryParse(value2.ToString(), val2)
Dim result As Decimal = 0
If val1 > 0 AndAlso val2 > 0 Then
result = CDec(val1) / val2 * 100
End If
Return result
End Function

Public Shared Function PercentOf(first As SByte, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As SByte, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Short, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Integer, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Long, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Double, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Decimal, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As SByte) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Short) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Integer) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Long) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Double) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Decimal) As Decimal
Return CalculatePercentOf(first, second)
End Function

Public Shared Function PercentOf(first As Single, second As Single) As Decimal
Return CalculatePercentOf(first, second)
End Function
```

# How to create a random number in javascript

To create a random number in javascript you can use the following snippet.

## Sample JS

```var random=Math.floor((Math.random() * 10) + 1);
```

Result
A random number between 1 and 10.

fore more informations see W3Schools JavaScript random() Method