Page 1 of 1

operatorius?

Posted: 2014 Dec 31 12:46
by Spag
Kaip suprasti operatoriu

Code: Select all

|=
??

Tarkim

Code: Select all

new shit shit = 0shit |= 524

Re: operatorius?

Posted: 2014 Dec 31 14:02
by MPD
tas pats kas shit = shit | 524, o | yra bitinis "arba". Tiek lentelė, tiek pavizdys duoti. http://en.wikipedia.org/wiki/Bitwise_op ... _.22.7C.22

Re: operatorius?

Posted: 2014 Dec 31 17:56
by hleV
Aš tai taip paprastai aiškinu: |= prideda bitus, &= išima bitus. Taip tu pvz. gali į vieną kintamąjį pridėt daug boolean reikšmių bei patikrint ar nors viena reikšmė yra true be jokio loop.

Code: Select all

enum *= 2{    BIT_1 = 1, // (1 << 0)    BIT_2, // 2 = (1 << 1)    BIT_3 // 4 = (1 << 2)}; Test(){    new bits = BIT_1 | BIT_2;    bits &= BIT_2;    bits |= BIT_3;     if (bits & BIT_1) server_print("BIT_1"); // Rodys, nes BIT_1 nustatem pradzioj    if (bits & BIT_2) server_print("BIT_2"); // Nerodys, nes BIT_2 pasalinom    if (bits & BIT_3) server_print("BIT_3"); // Rodys, nes BIT_3 pridejom    if (bits) server_print("BIT_1 or BIT_2 or BIT_3 is set"); // bits yra > 0 jei nors viena reiksme prideta}