C# 0 The Complete Reference


Nybble  class to implement a nybble data type. It uses  an int



Download 4,07 Mb.
Pdf ko'rish
bet323/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   319   320   321   322   323   324   325   326   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Nybble

 class to implement a nybble data type. It uses 

an

int

 for its underlying storage, but it restricts the values that can be held to 0 through 15. 

It defines the following operators:

•  Addition of a 



Nybble

 to a 


Nybble

•  Addition of an 



int

 to a 


Nybble

•  Addition of a 



Nybble

 to an 


int

•  Greater than and less than

•  The increment operator

• Conversion to 



Nybble

 from 


int

• Conversion to 



int

 from 


Nybble

These operations are sufficient to show how a class type can be fully integrated into the C# 

type system. However, for complete 

Nybble

 implementation, you will need to define all of 

the other operators. You might want to try adding others on your own.

www.freepdf-books.com




PART I

C h a p t e r   9 :  

O p e r a t o r   O v e r l o a d i n g  

241


PART IPART I

The complete 



Nybble

 class is shown here along with a 



NybbleDemo

, which 


demonstrates its use:

// 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 ++.



www.freepdf-books.com


242

 

P a r t   I :  



T h e   C #   L a n g u a g e

  public static Nybble operator ++(Nybble op)

  {

    Nybble result = new Nybble();



    result.val = op.val + 1;

    result.val = result.val & 0xF; // retain lower 4 bits

    return result;

  }


  // Overload >.

  public static bool operator >(Nybble op1, Nybble op2)

  {

    if(op1.val > op2.val) return true;



    else return false;

  }


  // Overload <.

  public static bool operator <(Nybble op1, Nybble op2)

  {

    if(op1.val < op2.val) return 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);

  }


}

class NybbleDemo {

  static void Main() {

    Nybble a = new Nybble(1);

    Nybble b = new Nybble(10);

    Nybble c = new Nybble();

    int t;

    Console.WriteLine("a: " + (int) a);

    Console.WriteLine("b: " + (int) b);

    // Use a Nybble in an if statement.

    if(a < b) Console.WriteLine("a is less than b\n");

    // Add two Nybbles together.

    c = a + b;

    Console.WriteLine("c after c = a + b: " + (int) c);

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   319   320   321   322   323   324   325   326   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish