Tuesday, November 25, 2014
Binary Number
namespace BinaryNumbersJackTalbert20NOV2014
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Clear();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip(this.txtDecValue1, "Please use whole integers i.e. 500 or 12345");
ToolTip1.SetToolTip(this.txtDecValue2, "Please use whole integers i.e. 500 or 12345");
ToolTip1.SetToolTip(this.txtDecimal, "Please use whole integers i.e. 500 or 12345");
ToolTip1.SetToolTip(this.txtBinary, "Please use binary numbers i.e. 11000011");
}
private void btnConvertToBinary_Click(object sender, EventArgs e)
{
//Use the Convert.ToString(int, base) method
int value;
if (int.TryParse(txtDecimal.Text, out value))
{
//Convert the value to base 2 (which is binary)
//The binary result is returns in a string type
string bitstring = Convert.ToString(value, 2);
//Display it
txtBinary.Text = bitstring.ToString();
}
else
{
MessageBox.Show("Please use the correct format; Decimals are all like integers - 1 2 3 4 and such");
Clear();
}
}
private void btnToDecimal_Click(object sender, EventArgs e)
{
//Get the bnary number from txtDecimal
int value =0 ;
string binary = txtBinary.Text;
//Convert to int
try
{
value = Convert.ToInt32(binary, 2);
//Display it
txtDecimal.Text = value.ToString();
}
catch
{
MessageBox.Show("Please use the correct format; Binary is like all '11000011' and such");
Clear();
}
}
private void btnAnd_Click(object sender, EventArgs e)
{
//Apply the AND (&) operation on the binary input
//1. Get input in decimal, convert to bin
int value1, value2;
if (int.TryParse(txtDecValue1.Text, out value1) && int.TryParse(txtDecValue2.Text, out value2))
{
ActivateTextBoxes();
//Convert to binary
string binary1 = Convert.ToString(value1, 2);
string binary2 = Convert.ToString(value2, 2);
//Display
txtBinValue1.Text = binary1.PadLeft(32, '0');
txtBinValue2.Text = binary2.PadLeft(32, '0');
//2. apply the AND Operation, display result in decimal and binary
int decResult = value1 & value2;
//Convert result to binary
string binResult = Convert.ToString(decResult, 2);
//Display
txtResultDec.Text = decResult.ToString();
txtResultBin.Text = binResult.PadLeft(32, '0');
}
else
{
MessageBox.Show("Please use the correct format");
Clear();
}
}
private void btnOr_Click(object sender, EventArgs e)
{
//Apply the AND ( | ) operation on the binary input
//1. Get input in decimal, convert to bin
int value1, value2;
if (int.TryParse(txtDecValue1.Text, out value1) && int.TryParse(txtDecValue2.Text, out value2))
{
ActivateTextBoxes();
//Convert to binary
string binary1 = Convert.ToString(value1, 2);
string binary2 = Convert.ToString(value2, 2);
//Display
txtBinValue1.Text = binary1.PadLeft(32, '0');
txtBinValue2.Text = binary2.PadLeft(32, '0');
//2. apply the AND Operation, display result in decimal and binary
int decResult = value1 | value2;
//Convert result to binary
string binResult = Convert.ToString(decResult, 2);
//Display
txtResultDec.Text = decResult.ToString();
txtResultBin.Text = binResult.PadLeft(32, '0');
}
else
{
MessageBox.Show("Please use the correct format");
Clear();
}
}
private void btnXor_Click(object sender, EventArgs e)
{
//Apply the AND ( ^ ) operation on the binary input
//1. Get input in decimal, convert to bin
int value1, value2;
if (int.TryParse(txtDecValue1.Text, out value1) && int.TryParse(txtDecValue2.Text, out value2))
{
ActivateTextBoxes();
//Convert to binary
string binary1 = Convert.ToString(value1, 2);
string binary2 = Convert.ToString(value2, 2);
//Display
txtBinValue1.Text = binary1.PadLeft(32, '0');
txtBinValue2.Text = binary2.PadLeft(32, '0');
//2. apply the AND Operation, display result in decimal and binary
int decResult = value1 ^ value2;
//Convert result to binary
string binResult = Convert.ToString(decResult, 2);
//Display
txtResultDec.Text = decResult.ToString();
txtResultBin.Text = binResult.PadLeft(32, '0');
}
else
{
MessageBox.Show("Please use the correct format");
Clear();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
Clear();
}
private void Clear()
{
txtBinary.Clear();
txtBinValue1.Clear();
txtBinValue2.Clear();
txtDecimal.Clear();
txtDecValue1.Clear();
txtDecValue2.Clear();
txtResultBin.Clear();
txtResultDec.Clear();
txtBinValue1.Enabled = false;
txtBinValue2.Enabled = false;
txtResultBin.Enabled = false;
txtResultDec.Enabled = false;
}
private void ActivateTextBoxes()
{
txtBinValue1.Enabled = true;
txtBinValue2.Enabled = true;
txtResultBin.Enabled = true;
txtResultDec.Enabled = true;
}
private void btnSetFourLowerBits_Click(object sender, EventArgs e)
{
//to set the first lower bits to 1, you need a mask number whose
//first 4 bits are set to 1 and the rest 28 bits to 0, the apply the OR (|)
//operation
//converting the mask binary to decimal result in 15
int value1;
int.TryParse(txtDecValue1.Text, out value1);
//convert both to binary to see its binary equivlent value
string binary1 = Convert.ToString(value1, 2);
//display
txtBinValue1.Text = binary1.PadLeft(32, '0');
//apply the OR operation on value1 to set (force) the first 4 bits to 1
int mask = 15; //the first 4 bits are set to 1
int result = value1 | mask;
//to observe the result in bits, convert to binary
string binResult = Convert.ToString(result, 2);
txtResultBin.Text = binResult.PadLeft(32, '0');
}
private void btnSetbit2thrubit5_Click(object sender, EventArgs e)
{
//bit2, bit3,bit4 and bit5 of value1, while keeping the rest unchange
//get value1
int value1;
int.TryParse(txtDecValue1.Text, out value1);
//convert both to binary to see its binary equivlent value
string binary1 = Convert.ToString(value1, 2);
//display
txtBinValue1.Text = binary1.PadLeft(32, '0');
//Assign a value to the mask so that bit2, bit3, bit4 and bit5 value1 are set to 1
//b7, b6, b5, b4, b3, b2, b1, b0
//0 0 1 1 1 1 0 0 => mask =
//Apply the appropriate logic operation to set these bits = AND Opearation
int mask = 60;
int result = value1 | mask;
//Display to see the results
//to observe the result in bits, convert to binary
string binResult = Convert.ToString(result, 2);
txtResultBin.Text = binResult.PadLeft(32, '0');
}
private void btnClearbits_Click(object sender, EventArgs e)
{
//clear bits 4,5,6,7 => use AND Operation
//b7 b6 b5 b4 b3 b2 b1 b0 must condisder all 32 bits bit31 down to bit0
//0 0 0 0 1 1 1 1 All the bits must be 1 except for bits 4,5,6,7
//
//The mask value corresponds to all bits 1 except for bits 4, 5, 6, 7 that are 0
int mask = (int)Math.Pow(2, 32) - 1 - (16 + 32 + 64 + 128);
int value1;
int.TryParse(txtDecValue1.Text, out value1);
//convert both to binary to see its binary equivlent value
string binary1 = Convert.ToString(value1, 2);
//display
txtBinValue1.Text = binary1.PadLeft(32, '0');
//Apply the mask to clear the bits 4,5,6,7
int result = value1 & mask;
//display
string binResult = Convert.ToString(result, 2);
txtResultBin.Text = binResult.PadLeft(32, '0');
}
}
}
//Lab Assignment: Complete the OR and XOR operations (11/20/2014)
//===================================================
//
//Friday 11/21 Lab Assignment
//****************************
//New Project
//Provide a user with a textbox(txtBinary) to enter a binary number (0's and 1's)
//Convert it to an int and display it in another textbox(txtInteger).
//If the number of bits is more than 32, throw a "TooLongBinaryException":
//
//use the binary input from txtBinary and set its first and last bits
//use the binary input in txtBinary and clear its first 3 bits and last 3 bits
//use the binary input in txtBinary and toggle bits 8 thru 15
//Write a method that takes an integer
//and returns the number of 0's in its binary equivalent value.
//ref: http://www.dotnetperls.com/binary-representation
Labels:
CSI-155
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment