feat: ✨ Made some bit shift (just to say)
This commit is contained in:
parent
c2d43c58b4
commit
03642a4aea
@ -1,3 +1,20 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using Marokanar;
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
SmurfByte sb = new SmurfByte(213);
|
||||
SmurfByte sb2 = new SmurfByte(148);
|
||||
SmurfByte sbOr = sb ^ sb2;
|
||||
Console.WriteLine(sbOr); // "11111101"
|
||||
Console.WriteLine(sb2); // "00011100"
|
||||
Console.WriteLine(sb); // "11100001"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Console.Write("Finished");
|
155
Marokanar/SmurfByte.cs
Normal file
155
Marokanar/SmurfByte.cs
Normal file
@ -0,0 +1,155 @@
|
||||
namespace Marokanar;
|
||||
|
||||
public class SmurfByte
|
||||
{
|
||||
private bool[] _sbits = new bool[8];
|
||||
|
||||
public SmurfByte(byte b)
|
||||
{
|
||||
int power = 1;
|
||||
for (int i = 7; i >= 0; i--)
|
||||
{
|
||||
_sbits[i] = (b & power) != 0;
|
||||
power *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
public bool GetBit(int i)
|
||||
{
|
||||
if (i < 0 || i > 7)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
else
|
||||
{
|
||||
int len = _sbits.Length - 1;
|
||||
return _sbits[len - i];
|
||||
}
|
||||
}
|
||||
|
||||
public void SetBit(int i, bool val)
|
||||
{
|
||||
if (i < 0 || i > 7)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
|
||||
_sbits[^(i + 1)] = val;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string result = String.Empty;
|
||||
foreach (var element in _sbits)
|
||||
{
|
||||
if (element == false)
|
||||
{
|
||||
result += "0";
|
||||
}
|
||||
else
|
||||
{
|
||||
result += "1";
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static SmurfByte operator ~(SmurfByte smurfbyte)
|
||||
{
|
||||
SmurfByte newSmurfByte = new SmurfByte(0);
|
||||
for (int i = 0; i < smurfbyte._sbits.Length; i++)
|
||||
{
|
||||
newSmurfByte._sbits[i] = !(smurfbyte._sbits[i]);
|
||||
}
|
||||
|
||||
return newSmurfByte;
|
||||
}
|
||||
|
||||
public static SmurfByte operator <<(SmurfByte smurfByte, int n)
|
||||
{
|
||||
SmurfByte newSmurfByte = new SmurfByte(0);
|
||||
for (int i = 0; i < smurfByte._sbits.Length - n; i++)
|
||||
{
|
||||
newSmurfByte.SetBit(i + n, smurfByte.GetBit(i));
|
||||
}
|
||||
|
||||
return newSmurfByte;
|
||||
}
|
||||
|
||||
public static SmurfByte operator >> (SmurfByte smurfByte, int n)
|
||||
{
|
||||
SmurfByte result = new SmurfByte(0);
|
||||
for (int i = smurfByte._sbits.Length - 1; i>= n; i--)
|
||||
{
|
||||
result.SetBit(i-n, smurfByte.GetBit(i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static SmurfByte operator |(SmurfByte smurfByte1, SmurfByte smurfByte2)
|
||||
{
|
||||
SmurfByte result = new SmurfByte(0);
|
||||
for (int i = 0; i < smurfByte1._sbits.Length; i++)
|
||||
{
|
||||
if (smurfByte1.GetBit(i) == true && smurfByte2.GetBit(i) == true)
|
||||
{
|
||||
result.SetBit(i, true);
|
||||
}
|
||||
|
||||
else if (smurfByte1.GetBit(i) == true || smurfByte2.GetBit(i) == true)
|
||||
{
|
||||
result.SetBit(i, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.SetBit(i, false);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static SmurfByte operator &(SmurfByte smurfByte1, SmurfByte smurfByte2)
|
||||
{
|
||||
SmurfByte result = new SmurfByte(0);
|
||||
for (int i = 0; i < smurfByte1._sbits.Length; i++)
|
||||
{
|
||||
if (smurfByte1.GetBit(i) == true && smurfByte2.GetBit(i) == true)
|
||||
{
|
||||
result.SetBit(i, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.SetBit(i, false);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static SmurfByte operator ^(SmurfByte smurfByte1, SmurfByte smurfByte2)
|
||||
{
|
||||
SmurfByte result = new SmurfByte(0);
|
||||
for (int i = 0; i < smurfByte1._sbits.Length; i++)
|
||||
{
|
||||
if (smurfByte1.GetBit(i) == true && smurfByte2.GetBit(i) == false)
|
||||
{
|
||||
result.SetBit(i, true);
|
||||
}
|
||||
else if (smurfByte1.GetBit(i) == false && smurfByte2.GetBit(i) == true)
|
||||
{
|
||||
result.SetBit(i, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.SetBit(i, false);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user