C# 0 The Complete Reference


if  statements, the value of  num



Download 4,07 Mb.
Pdf ko'rish
bet137/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   133   134   135   136   137   138   139   140   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

if

 statements, the value of 



num

 is ANDed with 1. If bit zero in 



num

 is set, the result 

of

num & 1

 is 1; otherwise, the result is zero. Therefore, the 



if

 statement can succeed only 

when the number is odd.

You can use the bit-testing capability of the bitwise 



&

 to create a program that uses the 

bitwise

&

 to show the bits of a 



byte

 value in binary format. Here is one approach:

// Display the bits within a byte.

using System;

class ShowBits {

  static void Main() {

    int t;

    byte val;

    val = 123;

    for(t=128; t > 0; t = t/2) {

      if((val & t) != 0) Console.Write("1 ");

www.freepdf-books.com




76

 

P a r t   I :  



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

      if((val & t) == 0) Console.Write("0 ");

    }

  }


}

The output is shown here:

0 1 1 1 1 0 1 1

The


for

 loop successively tests each bit in 



val

, using the bitwise AND, to determine if it is on 

or off. If the bit is on, the digit 

1

 is displayed; otherwise, 



0

 is displayed.

The bitwise OR can be used to turn bits on. Any bit that is set to 1 in either operand will 

cause the corresponding bit in the variable to be set to 1. For example

      1 1 0 1  0 0 1 1

|    1 0 1 0  1 0 1 0

      --------------------

      1 1 1 1  1 0 1 1

You can make use of the OR to change the make-even program shown earlier into a 

make-odd program, as shown here:

//  Use bitwise OR to make a number odd.

using System;

class MakeOdd {

  static void Main() {

    ushort num;

    ushort i;

    for(i = 1; i <= 10; i++) {

      num = i;

      Console.WriteLine("num: " + num);

      num = (ushort) (num | 1);

      Console.WriteLine("num after turning on bit zero: "

                        +  num + "\n");

    }

  }


}

The output from this program is shown here:

num: 1

num after turning on bit zero: 1



num: 2

num after turning on bit zero: 3

num: 3

num after turning on bit zero: 3



www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   133   134   135   136   137   138   139   140   ...   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