Create a 4-bit type called Nybble : Variable Definition « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Language Basics » Variable DefinitionScreenshots 
Create a 4-bit type called Nybble
Create a 4-bit type called Nybble

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Create a 4-bit type called Nybble. 
 
using System;  
  
// A 4-bit type. 
class Nybble {  
  int val; // underlying storage 
 
  public Nybble() { val = 0}  
 
  public Nybble(int i) { 
    val = i; 
    val = val & 0xF// retain lower 4 bits 
  
  
  // Overload binary + for Nybble + Nybble.  
  public static Nybble operator +(Nybble op1, Nybble op2)  
  {  
    Nybble result = new Nybble();  
  
    result.val = op1.val + op2.val;  
 
    result.val = result.val & 0xF// retain lower 4 bits  
  
    return result;  
  }  
  
  // Overload binary + for Nybble + int.  
  public static Nybble operator +(Nybble op1, int op2)  
  {  
    Nybble result = new Nybble();  
  
    result.val = op1.val + op2;  
 
    result.val = result.val & 0xF// retain lower 4 bits  
  
    return result;  
  }  
  
  // Overload binary + for int + Nybble.  
  public static Nybble operator +(int op1, Nybble op2)  
  {  
    Nybble result = new Nybble();  
  
    result.val = op1 + op2.val;  
 
    result.val = result.val & 0xF// retain lower 4 bits  
  
    return result;  
  }  
  
  // Overload ++. 
  public static Nybble operator ++(Nybble op
  
    op.val++; 
 
    op.val = op.val & 0xF// retain lower 4 bits 
 
    return op; 
  
 
  // Overload >. 
  public static bool operator >(Nybble op1, Nybble op2
  
    if(op1.val > op2.valreturn true
    else return false
  
 
  // Overload <. 
  public static bool operator <(Nybble op1, Nybble op2
  
    if(op1.val < op2.valreturn true
    else return false
  
 
  // Convert a Nybble into an int. 
  public static implicit operator int (Nybble op
  
    return op.val; 
  
 
  // Convert an int into a Nybble. 
  public static implicit operator Nybble (int op
  
    return new Nybble(op)
  
}  
  
public class NybbleDemo {  
  public static void Main() {  
    Nybble a = new Nybble(1);  
    Nybble b = new Nybble(10);  
    Nybble c = new Nybble();  
    int t; 
  
    Console.WriteLine("a: " (inta)
    Console.WriteLine("b: " (intb)
 
    // use a Nybble in an if statement 
    if(a < bConsole.WriteLine("a is less than b\n")
 
    // Add two Nybbles together 
    c = a + b; 
    Console.WriteLine("c after c = a + b: " (intc)
 
    // Add an int to a Nybble 
    a += 5
    Console.WriteLine("a after a += 5: " (inta)
 
    Console.WriteLine()
 
    // use a Nybble in an int expression 
    t = a * 3
    Console.WriteLine("Result of a * 2 + 3: " + t)
     
    Console.WriteLine()
 
    // illustrate int assignment and overflow 
    a = 19
    Console.WriteLine("Result of a = 19: " (inta)
     
    Console.WriteLine()
 
    // use a Nybble to control a loop     
    Console.WriteLine("Control a for loop with a Nybble.")
    for(a = 0; a < 10; a++
      Console.Write((inta + " ")
 
    Console.WriteLine()
  }  
}

           
       
Related examples in the same category
1.Declaring a variable.
2.Initializing a variable.
3.Variable default nameVariable default name
4.Heap and Stack Memory
5.Two reference type variables may refer (or point) to the same objectTwo reference type variables may refer (or point)
               to the same object
6.Use new with a value typeUse new with a value type
7.Init variableInit variable
8.An attempt to reference an uninitialized variable
9.Illustrates variable scope
10.Demonstrate the use of readonly variablesDemonstrate the use of readonly variables
11.Uninitialized Values
12.Int, float, double, decimalInt, float, double, decimal
13.Demonstrate dynamic initializationDemonstrate dynamic initialization
14.Demonstrate block scopeDemonstrate block scope
15.Demonstrate lifetime of a variableDemonstrate lifetime of a variable
16.This program attempts to declared a variable in an inner scope
17.Demonstrate castingDemonstrate casting
18.A promotion surpriseA promotion surprise
19.Using casts in an expressionUsing casts in an expression
20.Create an implication operator in C#Create an implication operator in C#
21.declaring a reference type variable and creating an object the variable will reference
22.Definite Assignment and ArraysDefinite Assignment and Arrays
23.Variable Scoping and Definite Assignment:Definite AssignmentVariable Scoping and Definite Assignment:Definite Assignment
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.