-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBitStringExtensions.cs
More file actions
62 lines (59 loc) · 1.89 KB
/
BitStringExtensions.cs
File metadata and controls
62 lines (59 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Runtime.CompilerServices;
using Platform.Random;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Platform.Collections
{
/// <summary>
/// <para>
/// Represents the bit string extensions.
/// </para>
/// <para></para>
/// </summary>
public static class BitStringExtensions
{
/// <summary>
/// <para>
/// Sets the random bits using the specified string.
/// </para>
/// <para></para>
/// </summary>
/// <param name="@string">
/// <para>The string.</para>
/// <para></para>
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetRandomBits(this BitString @string)
{
for (var i = 0; i < @string.Length; i++)
{
var value = RandomHelpers.Default.NextBoolean();
@string.Set(i, value);
}
}
/// <summary>
/// <para>
/// Creates a new BitString with random bits using the efficient constructor.
/// </para>
/// <para></para>
/// </summary>
/// <param name="length">
/// <para>The length of the bit string.</para>
/// <para></para>
/// </param>
/// <returns>
/// <para>A new BitString with random bits.</para>
/// <para></para>
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static BitString CreateWithRandomBits(long length)
{
var wordsCount = BitString.GetWordsCountFromIndex(length);
var randomArray = new long[wordsCount];
for (var i = 0L; i < wordsCount; i++)
{
randomArray[i] = RandomHelpers.Default.NextInt64();
}
return new BitString(randomArray, length);
}
}
}