C# 0 The Complete Reference


Bitwise Compound Assignments



Download 4,07 Mb.
Pdf ko'rish
bet141/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   137   138   139   140   141   142   143   144   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Bitwise Compound Assignments

All of the binary bitwise operators can be used in compound assignments. For example, the 

following two statements both assign to 

x

 the outcome of an XOR of 



x

 with the value 127:

x = x ^ 127;

x ^= 127;

www.freepdf-books.com



82

 

P a r t   I :  



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

The ? Operator

One of C#’s most fascinating operators is the 



?

, which is C#’s conditional operator. The 



?

operator is often used to replace certain types of if-then-else constructions. The 



?

 is called 

a

ternary operator

 because it requires three operands. It takes the general form



Exp1

 ? 


Exp2

 : 


Exp3

;

where 



Exp1

is a 


bool

 expression, and 



Exp2

 and 


Exp3

 are expressions. The type of 



Exp2

 and 


Exp3

 must be the same. Notice the use and placement of the colon.

The value of a 

?

 expression is determined like this: 



Exp1

 is evaluated. If it is true, then 



Exp2

 is evaluated and becomes the value of the entire 



?

 expression. If 



Exp1

 is false, then 



Exp3

 is evaluated, and its value becomes the value of the expression. Consider this example, 

which assigns 

absval

 the absolute value of 



val

:

absval = val < 0 ? -val : val; // get absolute value of val



Here, 

absval

 will be assigned the value of 



val

 if 


val

 is zero or greater. If 



val

 is negative, then 



absval

 will be assigned the negative of that value (which yields a positive value).

Here is another example of the 

?

 operator. This program divides two numbers, but will 

not allow a division by zero.

// Prevent a division by zero using the ?.

using System;

class NoZeroDiv {

  static void Main() {

    int result;

    for(int i = -5; i < 6; i++) {

      result = i != 0 ? 100 / i : 0;

      if(i != 0)

        Console.WriteLine("100 / " + i + " is " + result);

    }

  }


}

The output from the program is shown here:

100 / -5 is -20

100 / -4 is -25

100 / -3 is -33

100 / -2 is -50

100 / -1 is -100

100 / 1 is 100

100 / 2 is 50

100 / 3 is 33

100 / 4 is 25

100 / 5 is 20

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   137   138   139   140   141   142   143   144   ...   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