GURUH:651 22 TALABA:IKROMOV OYBEK Agar biz butun sonning n bitini o'zgartirmoqchi bo'lsak, i (bayt, qisqa, char, int yoki uzun): (i & 1 << n) != 0 // checks bit 'n’ i |= 1 << n; // sets bit 'n’ to 1 i &= ~(1 << n); // sets bit ‘n’ to 0 i ^ = 1 << n; // toggles the value of bit 'n'
22-bob: Bit bilan manipulyatsiya
Long/int/short/baytni bit niqobi sifatida ishlatish: - Long/int/short/baytni bit niqobi sifatida ishlatish:
- public class BitMaskExample {
- private static final long FIRST_BIT = 1L << 0;
- private static final long SECOND_BIT = 1L << 1;
- private static final long THIRD_BIT = 1L << 2;
- private static final long FOURTH_BIT = 1L << 3;
- private static final long FIFTH_BIT = 1L << 4;
- private static final long BIT_55 = 1L << 54;
- public static void main(String[] args) {
- checkBitMask(FIRST_BIT | THIRD_BIT | FIFTH_BIT | BIT_55);
- }
private static void checkBitMask(long bitmask) { - private static void checkBitMask(long bitmask) {
System.out.println("FIRST_BIT: " + ((bitmask & FIRST_BIT) != 0)); System.out.println("SECOND_BIT: " + ((bitmask & SECOND_BIT) != 0)); System.out.println("THIRD_BIT: " + ((bitmask & THIRD_BIT) != 0)); System.out.println("FOURTh_BIT: " + ((bitmask & FOURTH_BIT) != 0)); System.out.println("FIFTH_BIT: " + ((bitmask & FIFTH_BIT) != 0)); System.out.println("BIT_55: " + ((bitmask & BIT_55) != 0)); } } Chop etish FIRST_BIT: rost SECOND_BIT: noto’g’ri THIRD_BIT: rost FOURTh_BIT: noto’g’ri FIFTH_BIT: rost BIT_55: rost - biz checkBitMask parametri sifatida qabul qilgan niqobga mos keladi:FIRST_BIT | THIRD_BIT | FIFTH_BIT | BIT_55.
Do'stlaringiz bilan baham: |