本文整理汇总了C#中System.Collections.BitArray.Cast方法的典型用法代码示例。如果您正苦于以下问题:C# BitArray.Cast方法的具体用法?C# BitArray.Cast怎么用?C# BitArray.Cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.BitArray
的用法示例。
在下文中一共展示了BitArray.Cast方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetBitArray
public static bool[] GetBitArray(int x)
{
BitArray b = new BitArray(new int[] { x });
var bools = b.Cast<bool>();
//the visual representation is the opposite of how they bits actually go in the array so reverse things
return bools.Reverse().ToArray();
// bool[] bits = b.Cast<bool>().Select(bit => bit ? 1 : 0).ToArray();
}
示例2: ConvertToByte
internal static byte ConvertToByte(BitArray bits)
{
if (bits.Count != 8)
{
throw new ArgumentException("bits");
}
byte[] bytes = new byte[1];
var reversed = new BitArray(bits.Cast<bool>().Reverse().ToArray());
reversed.CopyTo(bytes, 0);
return bytes[0];
}
示例3: PopulatePrimes
private static void PopulatePrimes(int upTo)
{
if(upTo <= s_Primes.DefaultIfEmpty().Max())
return;
var bitArray = new BitArray(upTo*100);
bitArray[0] = false;
bitArray[1] = false;
for (var i = 2; i < bitArray.Length; i++)
if (bitArray[i])
for (var j = i * 2; j < bitArray.Length; j += i)
bitArray[j] = false;
s_Primes = bitArray.Cast<bool>().Select((x, y) => new { x, y }).Where(x => x.x).Select(x => x.y).ToList();
}
示例4: GetLogicalDrives_Windows_MatchesExpectedLetters
public void GetLogicalDrives_Windows_MatchesExpectedLetters()
{
string[] drives = Environment.GetLogicalDrives();
uint mask = (uint)GetLogicalDrives();
var bits = new BitArray(new[] { (int)mask });
Assert.Equal(bits.Cast<bool>().Count(b => b), drives.Length);
for (int bit = 0, d = 0; bit < bits.Length; bit++)
{
if (bits[bit])
{
Assert.Contains((char)('A' + bit), drives[d++]);
}
}
}
示例5: Run
//.........这里部分代码省略.........
sock.SendMore(BitConverter.GetBytes(host.numChunks));
sock.SendMore(BitConverter.GetBytes(host.chunkSize));
sock.SendMore(BitConverter.GetBytes(host.tarball.Files.Count));
foreach (var fi in files)
{
string fiName = fi.FullName.Substring(host.basePath.Length);
sock.SendMore(fiName, Encoding.Unicode);
sock.SendMore(BitConverter.GetBytes(fi.Length));
sock.SendMore(new byte[16]);
}
sock.Send("", Encoding.Unicode);
trace("{0}: Sent JOINED response", clientIdentity.ToString());
if (host.ClientJoined != null) host.ClientJoined(host, clientIdentity);
break;
case "NAKS":
// Receive the client's current NAK:
byte[] tmp = request.Dequeue();
if (tmp.Length != host.numBitArrayBytes)
{
trace("{0}: Bad NAKs", clientIdentity.ToString());
sock.Send("BADNAKS", Encoding.Unicode);
break;
}
sock.Send("", Encoding.Unicode);
lock (host.clientLock)
{
BitArray newNAKs = new BitArray(tmp);
// Add to the running ACK count the number of chunks turned from NAK to ACK in this update:
if (client.HasNAKs)
{
client.RunningACKCount += (
from i in Enumerable.Range(0, host.numChunks)
where (client.NAK[i] == true) && (newNAKs[i] == false)
select i
).Count();
}
else
{
client.RunningACKCount += newNAKs.Cast<bool>().Take(host.numChunks).Count(b => !b);
}
// Update to the new NAK state:
client.NAK = newNAKs;
// Inform the server to update the program:
host.haveNewNAKs = true;
}
trace("{0}: NAKs received", clientIdentity.ToString());
if (host.ChunksACKed != null) host.ChunksACKed(host);
break;
case "LEAVE":
sock.Send("LEFT", Encoding.Unicode);
lock (host.clientLock)
{
// Remove the client's state record:
host.clients.Remove(clientIdentity);
}
if (host.ClientLeft != null) host.ClientLeft(host, clientIdentity, ClientLeaveReason.Left);
trace("{0}: Client left", clientIdentity.ToString());
break;
case "ALIVE":
if (newClient)
{
trace("{0}: WHOAREYOU", clientIdentity.ToString());
byte[] sendIdentity = new byte[1] { (byte)'@' }.Concat(clientIdentity.ToByteArray()).ToArray();
sock.Send("WHOAREYOU", Encoding.Unicode);
break;
}
// Fantastic.
sock.Send("", Encoding.Unicode);
break;
default:
// Unknown command.
sock.Send("UNKNOWN", Encoding.Unicode);
trace("{0}: Unknown request", clientIdentity.ToString());
break;
}
request = null;
});
while (host.isRunning)
{
while (ctx.Poll(pollItems, 100000L) == 1)
{
}
}
}
}
示例6: Run
//.........这里部分代码省略.........
List<TarballEntry> tbes = new List<TarballEntry>(numFiles);
for (int i = 0; i < numFiles; ++i)
{
if (packet.Count == 0) { trace("FAIL!"); return ControlREQState.Nothing; }
string fiName = Encoding.Unicode.GetString(packet.Dequeue());
if (packet.Count == 0) { trace("FAIL!"); return ControlREQState.Nothing; }
long length = BitConverter.ToInt64(packet.Dequeue(), 0);
if (packet.Count == 0) { trace("FAIL!"); return ControlREQState.Nothing; }
byte[] hash = packet.Dequeue();
TarballEntry tbe = new TarballEntry(fiName, length, hash);
tbes.Add(tbe);
}
lock (tarballLock)
{
if (tarball != null)
{
tarball.Close();
tarball.Dispose();
tarball = null;
}
}
lock (tarballLock)
{
// Create the tarball reader that writes the files locally:
tarball = new TarballStreamReader(downloadDirectory, tbes);
// Get our local download state:
naks = getClientState(this, tarball);
}
ackCount = naks.Cast<bool>().Take(numChunks).Count(b => !b);
return new ZMQStateMasheen<ControlREQState>.MoveOperation(ControlREQState.SendNAKS, true);
}),
new ZMQStateMasheen<ControlREQState>.State(ControlREQState.SendNAKS, (sock, revents) =>
{
if (shuttingDown) return ControlREQState.Nothing;
if (nakBuf == null) return ControlREQState.Nothing;
// Send our NAKs:
ctl.SendMore(ctl.Identity);
ctl.SendMore("NAKS", Encoding.Unicode);
// TODO: RLE!
naks.CopyTo(nakBuf, 0);
trace("SEND NAK");
ctl.Send(nakBuf);
return ControlREQState.RecvNAKS;
}),
new ZMQStateMasheen<ControlREQState>.State(ControlREQState.RecvNAKS, (sock, revents) =>
{
Queue<byte[]> packet = ctl.RecvAll();
// Don't care what the response is for now.
return ControlREQState.Nothing;
})
);
// Disk-write acknowledgement poll input handler:
pollItems[2].PollInHandler += new PollHandler((Socket sock, IOMultiPlex revents) =>
{
byte[] idxPkt = sock.RecvAll().Dequeue();
示例7: Write
public void Write(byte value)
{
var bits = new BitArray(new byte[] { value });
foreach(var b in bits.Cast<bool>())
{
Write(b);
}
}