"& = ~ (1" PINXX ") dan foydalanib pinni qayta o'rnatish

sog'liqqa ogohlantirish: umumiy boshlang'ich

nima uchun bu pinni qayta tiklash uchun standart usul:

REGISTER_NAME &= ~(1 << PINXX);

to'g'ridan-to'g'ri emas:

REGISTER_NAME &= (0 << PINXX);

thanks in advance for time, answers & bibliographical hints

1
Buni hech kim tushunmaydi. Bu ongli o'quvchilar uchun savol.
qo'shib qo'ydi muallif Amit, manba
Ikkinchi usul sodda ko'rinmasa, REGISTER_NAME = 0 (bu birinchi usul bilan bir xil emas) deb aytadigan murakkab usulga o'xshaydi
qo'shib qo'ydi muallif npostavs, manba

5 javoblar

Buni tushunish kerak, har bir belgi nimani anglatadi

REGISTER_NAME &= ~(1 << PINXX);

1) << - this is logical shift left. This means that number on left side is shifted left by number of binary position, which is in right side.

For example. 1 << 5 means 0b00000001 shifted left 5 times (while zeroes appears at right). i.e. result will be 0b00100000 == 32.

In other words, 1 << X means "give a number, in which only bit #X is set, and other bits are zeros"

Ok. At this point you can see that 0 << anything gives always zero,

2) ~ - bu bitni rad etishdir. Raqamning ikkilamchi vakolatlaridagi har bir pozitsiyada har bitning inverts qiymati.

I.e. ~0b00100000 (or ~(1 << 5)) == 0b11011111.

Concluding, ~(1 << X) means "give a number, in which all bits are set, except of bit #X, which is to became zero"

3) &= - this is assignment combined with bitwise AND. I.e. X &= Y is the same that X = X & Y;

4) & - this is bitwise AND. result of this operation is number, in which only that bits are set, which are set at same position in both operands, and zero otherwise.

Buning ma'nosi, agar operandlardan birida alohida bit o'rnatilgan bo'lsa, natijada bir xil bit boshqa operandning bir xil pozitsiyasiga teng bo'ladi. Agar bu bit yoki operandda nol bo'lsa, natijada bir natija nol bo'ladi.

You can rephrase X &= Y in this way: "if a bit in Y is one, leave the bit at the same position in X unchanged (whatever it is - one or zero). If the bit in Y is zero, then clear the same bit in X."

As was said above, ~(1 << N) gives a number where every bit is set, except of N. So X &= ~(1 << N) means "Clear bit #N, and remain all other bits unchanged".

Endi siz buni ko'ra olasiz

REGISTER_NAME &= (0 << PINXX); 

is nonsense, because it means "Clear all the bits". It is the same as REGISTER_NAME = 0;

By the way, if you are using assignment in this form:

REGISTER_NAME |= (0 << PINXX) | (1 << PINYY);

bu "set bit PINYY va aniq bit PINXX" degan ma'noni anglatmaydi, "bit PINYY ni belgilash va bit PINXX bilan boshqa hech narsa qilmaslik (boshqa barcha bitlardagi kabi)" degan ma'noni anglatadi

2
qo'shib qo'ydi
juda yaxshi tushuntirilgan
qo'shib qo'ydi muallif MikeD, manba

Nima uchun nolga o'tish yanada "sodda" bo'lishi mumkin? Hech qanday mantiqqa o'rin yo'q, faqat bittadan bittadan bo'lsa, butun ro'yxatni o'chirib tashlaydi. Siz xohlagan narsalarni emas.

Shuni yodda tuting:

REGISTER &= ~(1 << PIN);

degani

REGISTER = REGISTER & ~(1 << PIN);

Ie. u bittadan-bittadan va bittadan PIN dan tashqari barcha pozitsiyalarda 1-raqamli sobit registrga ega bo'ladi. Ayniqsa ~ operatori faqat 1 emas, balki kaydırılmış qiymati uchun ham amal qiladi.

Bu faqat shu bitni 0 ga sozlash va boshqalarga tegmaslik ta'siriga ega. Siz istagan narsangiz, albatta, bu har doim ishlatilgan kod. :)

0
qo'shib qo'ydi
ajoyib, rahmat
qo'shib qo'ydi muallif mgd, manba

To set a bit, you variable |= (1 << position). This will generate something like 0000100, assuming position = 2.
To unset a bit, you do the reverse: you AND instead of OR, and the mask is the opposite of the other.

Pozitsiyani = 2 deb hisoblasangiz,

  1. 1 << position: 00000100 as before.
  2. Reverse it with bitwise not (~): 11111011.
  3. Apply bitwise AND. If it's `11011100, for example, you get:

    11011100 &
    11111011 =
    -----------
    11011000
    _____^____

Maqsad uchun men 8 bitli qiymatlarni qabul qildim, ammo protsedura ancha kengligi bilan o'zgarmaydi. Birinchi marta biroz qiyinlashishi mumkin, ammo agar siz o'zingiz hisob-kitob qilsangiz, ortda qoladigan mantiqqa to'g'ri kelishi kerak; Bu matematika.

0
qo'shib qo'ydi
juda foydali, rahmat
qo'shib qo'ydi muallif mgd, manba

birinchi ko'rsatma pin XX ga qayta tiklanadi, ikkinchi buyruqlar ushbu registrga taalluqli barcha pinlarni tiklaydi

0
qo'shib qo'ydi

Menimcha, chalkashliklar "standart" tarzda nima yuz berayotganligining noto'g'ri tavsifi berilganligidan kelib chiqadi.

Men sizga buni amalga oshirganingizda:

REGISTER_NAME &= ~(1 << PINXX);

~ 1 bilan, ya'ni "0" bilan "mantiqiy va PINXX holatidagi pin degan ma'noni anglatadi.

Lekin bu demak emas. Aslida nima demoqchi, reestrning qanchalik katta ekanligiga bog'liq. Men buni 16 bit deb o'ylayman va PINXX - 8 ni tashkil etadi.

Keyinchalik, aslida aytilgan standart yo'l nima:

REGISTER_NAME &= ~(1 << 8)

yoki (ikkitomonlama sonlar uchun 0b ni qabul qiladigan derivatingiz byoki deb hisoblasangiz)

REGISTER_NAME &= ~(0b0000000100000000)

yoki

REGISTER_NAME &= 0b1111111011111111

That is, the command isn't operating on just one pin - it's operating on all of them. (Just the operation on everything but the pin you want to change is "logically AND with 1", which means it is left alone). The expression ~(1 << PINXX) is just a way to construct a number that's all 1s in the places you don't want to touch, and 0 in the spot you do.

Sizning tavsiya etilgan "soddalashtirilgan" versiyangiz sizga quyidagilarga mos keladi:

REGISTER_NAME &= 0b0000000000000000

Ushbu registr tomonidan nazorat qilingan barcha pinlarni qayta tiklash.

0
qo'shib qo'ydi
o'ng. Rahmat - javoblar juda foydali bo'ldi, men qanday qilib uni tushunmasligim haqida juda achinarli bo'lishi mumkin, deb o'ylayman.
qo'shib qo'ydi muallif mgd, manba