■ 비트 단위 연산하기
------------------------------------------------------------------------------------------------------------------------
▶ MainForm.cs
using System; using System.Windows.Forms;
namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent();
Load += Form_Load; this.operandTextBox1.TextChanged += operandTextBox_TextChanged; this.operandTextBox2.TextChanged += operandTextBox_TextChanged; this.operatorComboBox.SelectedIndexChanged += operatorComboBox_SelectedIndexChanged; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event
#region 폼 로드시 처리하기 - Form_Load(sender, e)
/// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.operatorComboBox.SelectedIndex = 0; }
#endregion #region 피연산자 텍스트 박스 텍스트 변경시 처리하기 - operandTextBox_TextChanged(sender, e)
/// <summary> /// 피연산자 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void operandTextBox_TextChanged(object sender, EventArgs e) { Calculate(); }
#endregion #region 연산자 콤보 박스 선택 인덱스 변경시 처리하기 - operatorComboBox_SelectedIndexChanged(sender, e)
/// <summary> /// 연산자 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void operatorComboBox_SelectedIndexChanged(object sender, EventArgs e) { Calculate(); }
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 계산하기 - Calculate()
/// <summary> /// 계산하기 /// </summary> private void Calculate() { this.operandDecimalTextBox1.Clear(); this.operandDecimalTextBox2.Clear(); this.resultTextBox.Clear(); this.resultDecimalTextBox.Clear();
try { int operand1 = Convert.ToInt32(this.operandTextBox1.Text, 2); int operand2 = Convert.ToInt32(this.operandTextBox2.Text, 2);
int result = 0;
switch(this.operatorComboBox.Text) { case "&" : result = (operand1 & operand2); break; case "|" : result = (operand1 | operand2); break; case "^" : result = (operand1 ^ operand2); break; case "<<" : result = (operand1 << operand2); break; case ">>" : result = (operand1 >> operand2); break; case "~" : result = ~operand1; break; }
int length = Math.Max(operandTextBox1.Text.Length, operandTextBox2.Text.Length);
this.resultTextBox.Text = Convert.ToString(result, 2).PadLeft(length,'0');
this.operandDecimalTextBox1.Text = operand1.ToString(); this.operandDecimalTextBox2.Text = operand2.ToString(); this.resultDecimalTextBox.Text = result.ToString(); } catch { } }
#endregion } }
|
------------------------------------------------------------------------------------------------------------------------
'C# > Common' 카테고리의 다른 글
[C#/COMMON] CodeDomProvider 클래스 : 런타임에서 C# 코드를 동적으로 컴파일하기 (0) | 2020.08.09 |
---|---|
[C#/COMMON] Regex 클래스 : IsMatch 정적 메소드를 사용해 IPV4 주소 검증하기 (0) | 2020.08.09 |
[C#/COMMON] Regex 클래스 : IsMatch 정적 메소드를 사용해 이메일 주소 검증하기 (0) | 2020.08.09 |
[C#/COMMON] Regex 클래스 : IsMatch 정적 메소드를 사용해 특수 문자 사용 여부 구하기 (0) | 2020.08.09 |
[C#/COMMON] 극 좌표 → 직교 좌표 변환하기 (0) | 2020.08.08 |
[C#/COMMON] 비트 단위 연산하기 (0) | 2020.08.06 |
[C#/COMMON] 윈도우즈 프로세스 트리 표시하기 (0) | 2020.08.06 |
[C#/COMMON] List<T> 클래스 : ForEach 메소드 사용하기 (0) | 2020.07.22 |
[C#/COMMON] 각도 정규화하기 (0) | 2020.07.18 |
[C#/COMMON] Math 클래스 : IEEE 754 실수 계산하기 (0) | 2020.07.17 |
[C#/COMMON] Math 클래스 : 실수 올림/반올림/내림하기 (0) | 2020.07.17 |
댓글을 달아 주세요