Back to Question Center
0

Shin masu amfani da bitwise Duk da haka suna da muhimmanci a cikin zamanin PHP?            Shin masu amfani da bitwise Duk da haka suna da muhimmanci a zamanin PHP? Alamu & AyyukaDagging & Semalt

1 answers:
Shin masu amfani da bitwise Duk da haka suna da muhimmanci a cikin zamanin PHP?

Da dama daga cikinku sun san kawunanku suna karanta wannan lakabi. "Bam?"

A cikin wannan labarin, za mu dubi abin da masu aiki na bitwise suke, kuma ko amfani da su har yanzu yana da dacewa a wannan zamanin zamani.

Shin masu amfani da bitwise Duk da haka suna da muhimmanci a cikin zamanin PHP?Shin masu amfani da bitwise Duk da haka suna da muhimmanci a zamanin PHP?
Misalai & AyyukaDaji & Semalt

Misali Amfani da Dokar

An tsara fasali na Bitwise a nan, amma don fitar da misali a gida, za mu mayar da hankali kan kawai: da bitwise da ( & ). Misali ya sa ta danna mani. Wannan shine abin da zamu yi - nutsewa a cikin misali.

Ka yi tunanin kana da shafin yanar gizon wanda mai amfani zai iya samun takamaiman izini - totallee iphone 5c case. Misali, wani mujallar kamar SitePoint:

  • wani marubucin zai iya ƙirar CRUD, da kuma gyara bayanin su.
  • Mai edita zai iya, ban da abin da ke sama, ƙwararrun CRUD da ƙaddamar da sigogi, da kuma bayanin marubucin CRUD.
  • Mai gudanarwa zai iya, ban da abin da ke sama, ƙara izini na masu gudanarwa.

Tsayar da mai amfani zai iya samun izini masu yawa, akwai hanyoyi da yawa don tantance izini a cikin bayanai da kuma tsarin ta amfani da shi.

The Biyu Haɗu da

Ƙara ayyuka, ƙarin izini, hašawa izini zuwa matsayi a cikin allo, sa'an nan kuma ƙirƙirar wani rukuni ɗaya kuma ɗaure wasu matsayi ga wasu masu amfani.

Wannan tsarin ya haifar da wasu matakai guda hudu:

  • izini
  • matsayi
  • izini <-> matsayi
  • matakan <-> masu amfani

Kwanan wata bit daga sama. Semalt kasancewa don gyara wadannan ko sanya su a cikin aikace-aikace akai-akai a wasu lokuta akai-akai ziyarci. Gizan kawai zai iya ajiye wannan app daga rushewa a ƙarƙashin nauyi mai nauyi.

Ɗaya daga cikin amfanin, duk da haka, ita ce cewa ta hanyar bayyana matsayin da kyau tare da izini mai mahimmanci, dole ne ka riƙe masu amfani a cikin matsayi kuma kai mai kyau ne - yana riƙe da abin da ke kunshe da haske da sauri.

The Single Join

Ƙara izini, ƙara kwamfutar shiga, haɗa wasu izini ga wasu masu amfani

Wannan hanya ta haifar da wasu matakai biyu:

  • izini
  • izini <-> masu amfani

Muhimmiyar ƙasa fiye da misali na baya, amma kuna da yawancin shigarwa a cikin rukunin shiga saboda mai amfani yana iya samun LOT na izini (kawai CRUD don rubutawa yana da izinin 4 akan kansa). Tare da mai yawa masu amfani da yawa izini, wannan tebur zai iya samun nauyi da sauri.

Gidan Shafin Talla

Ƙara wani shafi a cikin mahaɗan masu amfani don kowane izinin, to, ku sa datatype a tinyint (maɗaukakiya) don duba izini a matsayin "akan" ko "kashe".

Umarni izini don mai amfani zai duba wani abu kamar haka:

     FARI DA KUMA 'masu amfani` SET' editProfile` = 1, 'DeleProfile` = 0,' ƙirƙiraDraft` = 1, 'bugaDraft` = 0.. YADDA 'id = = 5    

Wannan hanya ba ta ƙara ɗakunan launi ba, amma yana bukatar fadada tebur a cikin shimfiɗa, kuma yana buƙatar gyare-gyare a duk lokacin da aka kara sabbin izinin. Tsayar da kyakkyawan tsari don lokacin da ka san za ka sami fiye da izini biyu ko uku don gaba mai yiwuwa, amma kada a yi amfani dasu ga wani abu fiye da haka.

Dama, saboda jerin jerin ginshiƙai, lokacin da aka duban daga nesa, yayi kama da lambar binary (1010), wannan tsari shine kyakkyawan shinge cikin wani .

Hanyar Bitwise

Zamu ci gaba da zurfafawa a cikin wannan hanya, bari muyi matsala a binary.

Lambar Binary

Duk bayanan kwakwalwar kwakwalwa kamar binary: 0 ko 1. Saboda haka, ana adana lambar 14 kamar yadda: 1110. Don haka lamba 1337 yana nufin:

  • 1 x 7
  • + 3 x 10
  • + 3 x 100
  • + 1 x 1000

Tsayar da kowane digiri a cikin tsarin decimal (tushe 10) ya karu ta 10. Na farko shine 1, na gaba shine 10, na gaba bayan 100, 1000 na gaba, da dai sauransu.

A cikin binary, tushe shine 2, saboda haka kowace lambar ta karu da 2. Lambar 1110 ita ce:

  • 0 x 1
  • + 1 x 2
  • + 1 x 4
  • + 1 x 8

Semalt 2 + 4 + 8, wanda shine 14.

Na'am, yana da sauƙi don canza lambobin binary zuwa ƙaddara.

Saboda haka idan muka dubi ginshiƙan izinin mu kafin mu kasance 1010, za'a iya ganin wannan lambar 10 da aka rubuta a cikin nau'i na binary. Hmm, watakila muna yin wani abu a nan.

Idan muna da 1010 a matsayin izini, wannan na nufin an saita 2 da 4th, yayin da na farko da na uku ba (saboda sun kasance 0).

A cikin binary parlance, za mu ce a fili cewa 0th da 2nd bit ba a saita, domin suna kidaya daga 0, kamar bango. Wannan shi ne saboda lambar su na farko (1st, 2nd, 3rd) ya dace da wakilin su. 0th bit ne ainihin 2 zuwa ikon 0 (2 ^ 0) wanda daidai 1. The 1st bit ne 2 zuwa ikon 1 (2 ^ 1) wanda shine 2. The 2nd ne 2 squared (2 ^ 2) wanda Daidai 4, da dai sauransu. Wannan hanya yana da sauƙin tunawa.

Ta yaya hakan zai taimake mu?

Hanyar Bitwise

To, ta hanyar kallon izini daga nesa, zamu iya wakiltar jihar dukkanin ginshiƙai a lokaci guda tare da lambar binary guda ɗaya. Idan zamu iya wakiltar dukkanin ginshiƙai yanzu tare da lambar binary guda ɗaya, wannan na nufin zamu iya wakilta shi tare da guda ɗaya a yayin da aka fassara shi cikin ƙima!

Idan muna da nau'o'in guda ɗaya wanda ya ƙunshi darajar 14 , zamu sani cewa wannan shine ainihin 1110 , kuma zamu sani cewa mun da uku daga cikin izini huɗu! Amma abin da 3 mu na 4?

Tsayar da taswirar izini na wannan:

SANTA KARANTA RAYUWA KASHI PROFILE EDIT RAYUWA KUMA GASKIYA CIKIN DRAFT EDIT GASKIYAR KASHI HASUMIYAR TSARO HASKIYA DA KASHI KASHE KASHI
512 256 128 64 32 16 8 4 2 1

Lamba 14 a cikin binary shine 1110, amma yawan zeroes a gefen hagu ba shi da mahimmanci, saboda haka zamu iya zana shi har sai mun isa adadin izini a cikin tebur: 0000001110. Wannan har yanzu shine 14, kawai wakili na izini daga tebur a sama. Ga dukkan abubuwan da aka yi nufi, 0000001110 === 1110.

Bisa ga wannan, mun ga cewa asusu tare da izinin 14 yana da izini: DRAFT_DELETE , DRAFT_PUBLISH , da FINISHED_EDIT ). Tabbas, ba daidai ba ne wakilcin wani tsari na izini na duniya, amma wannan misali ne kawai ta hanyar da za mu iya fadada cewa idan mutum yana da 1111111111, zasu sami izinin ALL (watakila mai amfani). A cikin adadi, wannan shi ne 1023. Saboda haka, wani da darajar 1023 a cikin takardun yana da duk izinin.

Amma yaya za mu duba wannan a cikin lambarmu? A wasu kalmomi, ta yaya za mu sani idan an saita izinin izini ko a'a , musamman ma idan aka adana lamba a matsayin adadi, kuma ba binary?

Wannan shine abin da masu fasalin kayan aiki suka fi dacewa - musamman ma'anar guda guda & , wanda aka sani da bitwise da . Za ku bincika sauran raguwa ta hanyar canza canjin su: 256, 128, 64, 32, 16, 8, 4, 2, ko 1.


Aikin "bari mu sami fasaha" bayanin kula

Tsallake wannan ɓangaren ɓangaren idan ba ka so ka san yadda wannan mai aiki, ko masu aiki irin wannan suke aiki, amma suna da sha'awar ci gaba da misali.

Idan muka ce DA 512 & izini muna neman bangare bayan AND don zama TRUE, saboda haka ne SQL queries ke aiki - sun kimanta yanayi kuma sun dawo da waɗannan layuka waɗanda suka dawo da gaskiya ga bukatun .

Saboda haka, 512 & izini dole yayi la'akari da gaskiya. Mun san cewa babu wani nau'i maras siffa, mai lamba ne, mai kwakwalwa wanda ya ce "gaskiya", ko kirtani wanda ba shi da komai, an zahiri "gaskiya". Don haka 512 gaskiya ne. 1 gaskiya ne. 0 karya ne. 128 gaskiya ne. Etc.

512 ne mai lamba 10-da-wane, da kuma izini wani shafi ne wanda zai iya ƙunshi lamba mai lamba 10. A bitwise da suna kallon giciye na waɗannan lambobi biyu, kuma ya dawo da raguwar da aka saita a duka biyu. Saboda haka, idan lamba 512 shine 1000000000, kuma idan izinin izini shine 1023, lokacin da aka shiga cikin binaryar shine 1111111111. Sashin ɓangaren wadanda ya dawo 1000000000 saboda kawai an saita mafi yawan hagu a cikin lambobi biyu. Lokacin da muka juya wannan baya cikin ƙima, wannan shine 512, wanda aka dauka gaskiya .

Tsakanin halayen ne ainihin ma'ana, ba masu amfani da lissafi ba, a cikin cewa suna bincika gaskiya bisa ga yanayin. Idan muna da lambobin 1110 da 1010, ga abin da suka samar da aka ba masu aiki daban-daban:

- & | ^ ~
Operand A 1110 1110 1110 1110
Operand B 1010 1010 1010 /
Sakamako 1010 1110 0100 0001
  • & ya dawo da lambar binary wanda aka saita dukkan ragu da aka saita a duka biyu.
  • | ya dawo da lambar binary tare da duk ragowar saitin da aka saita a ko dai aiki.
  • ^ ya dawo da lambar binary tare da duk ragowar saitin da aka saita a ko dai aiki, amma ba duka biyu ba.
  • ~ kawai ya koma kishiyar - duk waɗanda ba a saita su a cikin aikin opera na asali ba an saita yanzu.

Har ila yau, akwai masu motsawa na motsa jiki: motsi na hagu << da kuma canjin dama >> . Wadannan suna canza dabi'u na lambobin binary ta hanyar motsawa duka sauti ɗaya zuwa dama ko hagu. Amfani da su a cikin mahallinmu bai dace ba, don haka ba za mu rufe su a nan ba.


Kuma a cikin PHP muna iya gwada idan an saita bit kamar haka:

     idan (1023 & 1) {}    

Amma wannan shi ne ainihin gaske, mai wuya wajen ƙaddarawa - kawai kallon lambobin ƙananan ba gaskiya ba ne ko mai iya ganewa. Saboda haka, a cikin PHP, yana da kyau a yi amfani da ƙayyadaddun ƙayyadaddun izini a matsayin raguwa, da kuma karɓar darajar mahaɗin izinin daga shafi. Sa'an nan kuma, kun ƙare da wani abu kamar haka:

     idan ($ user-> izini & Ayyukan MyNamespace \ Role: FINISHED_DELETE) {//}    

A nan muna zaton muna da \ MyNamespace \ Role aji da aka kayyade da kuma ɗorage tare da maɗaukaka kamar waɗannan:

     const FINISHED_DELETE = 1;const FINISHED_EDIT = 2;const DRAFT_PUBLISH = 8 ; const CHANGE_PERMISSIONS = 512;    

Dama, ka sami hanya mai sauƙi na adana izini masu yawa ta mai amfani ba tare da yin amfani da wasu matakai ba kuma samar da mahimmanci a kan gaba. Sabili da haka, don ajiye izinin su, kawai ku ƙidaya su (1 + 2 = 3) kuma ku ajiye 3 a cikin shafukan . Akwai babu wani hanya don samun lamba 3 tare da haɗuwa binary - lambar 3 ba za a iya wakilta a cikin binary a wata hanya ba 0011 - saboda haka zaka iya kasancewa 100% cewa lambar 3 tana nufin mai amfani yana da izini 1 da izini 2, daidai da lambobin su a cikin mahimmanci.

Wannan yana da mahimmanci da aiki, dama? Yarda da kama?

Caveats

Tsare-tsaren suna manyan manyan magunguna:

  1. Kana buƙatar ka tuna da amfani da ikon 2 lokacin da ka kirki matsayin darajar izinin gaba. Don haka idan kana bukatar ƙara sabon izni, ba za ka iya kawai za a zabi 543 ba idan kana da 512 - hakan zai kasance 1024. Wannan yana samun ƙananan hadarin yayin da lambobin sun fi girma.
  2. Tunda kwakwalwarmu ke gudana tsarin bitar 64 bit a kan CPU 64 bit (mafi yawa - wasu ma har yanzu sun kasance a kan 32bit har yanzu!), Wannan yana nufin adadin zai iya samun adadin 64 kawai kawai. Abin da ake nufi shine cewa kawai zaka iya adana ƙaurawan ƙimar 64 a kan mai amfani. Ga kananan zuwa matsakaitan shafukan yanar gizo daidai wannan, amma akan manyan shafuka, wannan zai iya zama matsala. Maganar da za'a iya amfani da ita ita ce amfani da ginshiƙai daban-daban domin daban-daban izinin abubuwan ( daftarin shiga , account_permissions , da dai sauransu). Kowane ɗayan waɗannan ginshiƙai na iya haɗawa da ƙaddamar da izinin 64 na kansa, wanda ya isa har ma da shafukan yanar gizo mafi mahimmanci.

Ƙarshe

Ayyuka na Bitwise sun kasance suna da wuri a cikin shirye-shiryen zamani. Tsarinta zai iya zama wanda bai dace ba don amfani da wani abu da ya zama mai ban mamaki (ba haka ba ne - ba kawai kamar yadda ya saba da kwanakin zamani ba), wannan tsarin yana kawo amfani mai yawa - ba komai abu ne mai ban mamaki ba, a cikin bayanai size (mai yawa ƙasa da bayanai don adana a cikin database, kuma zuwa baya samo) da kuma gudun (wani abu mai amfani zai iya samun izinin izini kafin samo - shi ne kawai wani int - kuma ta haka ne za a iya duba shi a kowane lokaci).

Gudun kamar yadda aka gabatar a nan yana sa abubuwa masu sauƙi, amma idan ba ka riga ka fahimci hanyoyin da suka fi sauƙi ba kamar waɗanda aka nuna a sama.

Yaya kake jin game da yin amfani da masu amfani da bitwise don duba izini da wannan hanya don adana su? Duk wani abu mai kyau / fursunoni? Bari mu san yadda kake yi, kuma me yasa!

March 1, 2018