public static byte[] ShiftBitsRight([NotNull] this byte[] input, int shiftCount, out byte[] carry) { if (input == null) { throw new ArgumentNullException(nameof(input)); } var bytes = ByteArrayUtilities.CreateFilledByteArray(input.Length, 0x00); carry = input.GetBits(input.Length * 8 - shiftCount, shiftCount); // fill carry bits var bitWriteIndex = shiftCount; for (var bitReadIndex = 0; bitReadIndex < bytes.Length * 8 - shiftCount; bitReadIndex++, bitWriteIndex++) { var bitMask = (byte)(0x80 >> bitReadIndex % 8); // bit mask built from current bit position in byte if ((input[bitReadIndex / 8] & bitMask) > 0) // bit is set { bytes[bitWriteIndex / 8] |= (byte)(0x80 >> bitWriteIndex % 8); } } return(bytes); }
/// <exception cref="ArgumentException">Routing prefix is out of range</exception> private void InitSubnetFromIPAndRoute([NotNull] IPAddress ipAddress, [CanBeNull] int?routingPrefix) { if (ipAddress == null) { throw new ArgumentNullException(nameof(ipAddress)); } // determine address type var isIPv4 = ipAddress.AddressFamily == AddressFamily.InterNetwork; var isIPv6 = ipAddress.AddressFamily == AddressFamily.InterNetworkV6; if (!isIPv4 && !isIPv6) { throw new ArgumentException("IP Address must be IPv4 or IPv6", nameof(ipAddress)); } this.RoutingPrefix = routingPrefix ?? (isIPv4 ? IPv4BitCount : IPv6BitCount); // assign routing prefix // validate routing prefix if (this.RoutingPrefix < 0 || (isIPv4 && this.RoutingPrefix > IPv4BitCount) || (isIPv6 && this.RoutingPrefix > IPv6BitCount)) { throw new ArgumentException("Routing prefix is out of range", nameof(routingPrefix)); } var addressBytes = ipAddress.GetAddressBytes(); // get the byte of the ip address var addressByteLength = addressBytes.Length * 8; // get the number of bytes in the address // calculate net mask var netmaskBytes = ByteArrayUtilities.CreateFilledByteArray(isIPv6 ? 16 : 4) .ShiftBitsLeft(addressByteLength - this.RoutingPrefix); this.Netmask = new IPAddress(netmaskBytes); // assign net mask as ip address var networkBytes = addressBytes.BitwiseAnd(netmaskBytes); this.NetworkPrefixAddress = new IPAddress(networkBytes); var broadcastBytes = addressBytes.BitwiseOr(netmaskBytes.BitwiseNot()); this.BroadcastAddress = new IPAddress(broadcastBytes); this._length = BigInteger.Pow(2, (isIPv4 ? IPv4BitCount : IPv6BitCount) - this.RoutingPrefix); }
public static byte[] BitwiseNot([NotNull] this byte[] input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } var result = ByteArrayUtilities.CreateFilledByteArray(input.Length, 0x00); for (var i = 0; i < input.Length; i++) { result[i] = (byte)~input[i]; } return(result); }
public static byte[] GetBits([NotNull] this byte[] sourceBytes, int start, int?length = null) { if (sourceBytes == null) { throw new ArgumentNullException(nameof(sourceBytes)); } var sourceBitLength = sourceBytes.Length * 8; // the total number of bits in the source var readLength = length ?? sourceBitLength - start; // the length of bits that will be read if (readLength > sourceBitLength) { throw new InvalidOperationException("can't capture more bits than present"); } var writeBytesLength = (int)System.Math.Ceiling((double)readLength / 8); // the length of the writable bytes var writeBytes = ByteArrayUtilities.CreateFilledByteArray(writeBytesLength, 0x00); // destination array var destinationBitIndex = 0; // the bit index of the destination for (var i = 0; i < readLength; i++, destinationBitIndex++) { var sourceBitIndex = start + readLength - 1 - i; // bit index of the source var bitMask = (byte)(0x80 >> sourceBitIndex % 8); // bit mask built from current bit position in byte if ((sourceBytes[sourceBitIndex / 8] & bitMask) > 0) // bit is set { writeBytes[destinationBitIndex / 8] |= (byte)(0x01 << destinationBitIndex % 8); } } return(writeBytes.Reverse() .ToArray()); }
public byte[] CreateFilledArrayDefaultsTo0xFF(int size) => ByteArrayUtilities.CreateFilledByteArray(size);
public byte[] CreateFilledArrayTests(int size, byte intializer)//TODO: test necessary? => ByteArrayUtilities.CreateFilledByteArray(size, intializer);