C# 0 The Complete Reference


The Bitwise AND, OR, XOR, and NOT Operators



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

The Bitwise AND, OR, XOR, and NOT Operators

The bitwise operators AND, OR, XOR, and NOT are 



&

,

|

,

^

, and 


~

. They perform the same 

operations as their Boolean logic equivalents described earlier. The difference is that the 

bitwise operators work on a bit-by-bit basis. The following table shows the outcome of each 

operation using 1s and 0s:

p

q



p & q

p | q


p ^ q

~p

0



0

0

0



0

1

1



0

0

1



1

0

0



1

0

1



1

1

1



1

1

1



0

0

In terms of its most common usage, you can think of the bitwise AND as a way to turn 



bits off. That is, any bit that is 0 in either operand will cause the corresponding bit in the 

outcome to be set to 0. For example

      1 1 0 1  0 0 1 1

&   1 0 1 0  1 0 1 0

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

      1 0 0 0  0 0 1 0

Operator

Result


&

Bitwise AND

|

Bitwise OR



^

Bitwise exclusive OR (XOR)

>>

Shift right



<<

Shift left

~

One’s complement (unar y NOT)



T

ABLE


 4-1 

The Bitwise Operators

www.freepdf-books.com



74

 

P a r t   I :  



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

The following program demonstrates the 



&

 by using it to convert odd numbers into 

even numbers. It does this by turning off bit zero. For example, the low-order byte of the 

number 9 in binary is 0000 1001. When bit zero is turned off, this number becomes 8, 

or 0000 1000 in binary.

// Use bitwise AND to make a number even.

using System;

class MakeEven {

  static void Main() {

    ushort num;

    ushort i;

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

      num = i;

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

      num = (ushort) (num & 0xFFFE);

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

                        +  num + "\n");

    }


  }

}

The output from this program is shown here:



num: 1

num after turning off bit zero: 0

num: 2

num after turning off bit zero: 2



num: 3

num after turning off bit zero: 2

num: 4

num after turning off bit zero: 4



num: 5

num after turning off bit zero: 4

num: 6

num after turning off bit zero: 6



num: 7

num after turning off bit zero: 6

num: 8

num after turning off bit zero: 8



num: 9

num after turning off bit zero: 8

www.freepdf-books.com



Download 4,07 Mb.

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