C# 0 The Complete Reference


PART I C h a p t e r   4



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


PART I

C h a p t e r   4 :  

O p e r a t o r s  

81

PART IPART I



    Console.WriteLine("Value of n: " + n);

    // Multiply by 2.

    n = n << 1;

    Console.WriteLine("Value of n after n = n * 2: " + n);

    // Multiply by 4.

    n = n << 2;

    Console.WriteLine("Value of n after n = n * 4: " + n);

    // Divide by 2.

    n = n >> 1;

    Console.WriteLine("Value of n after n = n / 2: " + n);

    // Divide by 4.

    n = n >> 2;

    Console.WriteLine("Value of n after n = n / 4: " + n);

    Console.WriteLine();

    // Reset n.

    n = 10;

    Console.WriteLine("Value of n: " + n);

    // Multiply by 2, 30 times.

    n = n << 30; // data is lost

    Console.WriteLine("Value of n after left-shifting 30 places: " + n);

  }

}

The output is shown here:



Value of n: 10

Value of n after n = n * 2: 20

Value of n after n = n * 4: 80

Value of n after n = n / 2: 40

Value of n after n = n / 4: 10

Value of n: 10

Value of n after left-shifting 30 places: -2147483648

Notice the last line in the output. When the value 10 is left-shifted 30 times, information 

is lost because bits are shifted out of the range of an 

int

. In this case, the garbage value 

produced is negative because a 1 bit is shifted into the high-order bit, which is used as a 

sign bit, causing the number to be interpreted as negative. This illustrates why you must be 

careful when using the shift operators to multiply or divide a value by 2. (See Chapter 3 for 

an explanation of signed vs. unsigned data types.)




Download 4,07 Mb.

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