An Warware: Lambar Hagu_shift NumPy Lokacin da abubuwan da aka shigar da shift su ne lambobi

NumPy muhimmin ษ—akin karatu ne ga kowane mai tsara shirye-shiryen Python da ke aiki tare da bayanan lissafi ko ฦ™ididdiga. Yana ba da damar tsararru mai ฦ™arfi da ikon sarrafa matrix. ฦŠaya daga cikin ฦ™ananan sanannun ayyukan da NumPy ke bayarwa shine ikon yin ayyuka na bitwise akan abubuwan tsararru. Wannan ya haษ—a da aikin motsi na hagu na bitwise, waษ—anda za a iya amfani da su zuwa lambobi a cikin tsararrun NumPy. A cikin wannan labarin, za mu bincika aikin NumPy left_shift kuma mu samar da koyawa ta mataki-mataki kan yadda ake amfani da wannan aikin mai amfani.

Da farko, bari mu tattauna matsalar da muke son magancewa. Idan aka ba da abubuwan shigar da lamba biyu, muna so mu yi aikin motsi na hagu na bitwise akan kowace lamba, yadda ya kamata mu canza rago a cikin kowace lamba ฦ™ayyadaddun adadin matsayi zuwa hagu. A cikin Python, NumPy yana ba da aikin 'hagu_shift()' don wannan dalili, kuma za mu bincika yadda ake amfani da shi yadda ya kamata.

Fahimtar aikin NumPy left_shift()

Da ``NumPy hagu_shift()`Aiki yana ษ—aukar gardama guda biyu: tsararru mai ษ—auke da lambobi da za a canjawa da adadin matsayi don matsawa raฦ™uman ruwa. Sa'an nan aikin ya dawo da sabon tsararru inda kowane kashi aka bar shi bitwise ta hanyar ฦ™ayyadadden adadin matsayi. Gaba ษ—aya tsarin aikin shine kamar haka:

import numpy as np
result = np.left_shift(array, shift_positions)

Yanzu, bari mu nutse cikin koyawa ta mataki-mataki kan yadda ake aiwatar da wannan lambar don magance matsalarmu.

Koyawa ta mataki-mataki: Canjin Hagu na Bitwise tare da NumPy

1. Da farko, muna buฦ™atar shigo da ษ—akin karatu na NumPy. Ana iya yin haka ta ฦ™ara layin lamba mai zuwa:

   import numpy as np
   

2. Na gaba, za mu ฦ™irฦ™iri array na NumPy mai ษ—auke da lambobi masu ฦ™ima waษ—anda muke son aiwatar da aikin motsi na hagu na bitwise. Ana iya yin haka kamar haka:

   input_numbers = np.array([1, 2, 3, 4, 5])
   

3. Yanzu, za mu iya yin bitwise hagu aiki motsi ta amfani da `hagu_shift()` aikin. A cikin wannan misali, za mu matsar da ragowa a kowane lamba matsayi biyu zuwa hagu:

   result = np.left_shift(input_numbers, 2)
   

4. A ฦ™arshe, bari mu buga jerin abubuwan da aka samu don ganin sakamakon aikin motsi na hagu:

   print("Input numbers: ", input_numbers)
   print("Result after left shift: ", result)
   

Da fitarwa zai zama:

โ€œโ€œ
Lambobin shigarwa: [1, 2, 3, 4, 5]
Sakamako bayan motsi na hagu: [4, 8, 12, 16, 20]
โ€œโ€œ

Aikace-aikace da Ayyuka masu alaฦ™a

Ayyukan Bitwise kamar aikin NumPy left_shift suna da amfani a wurare da yawa, kamar sarrafa siginar dijital, sarrafa hoto, da cryptography. Suna ba masu shirye-shirye damar yin amfani da su da kuma nazarin bayanai a matakin binary, wanda zai iya zama mahimmanci don inganta aiki a wasu aikace-aikace.

Baya ga aikin hagu_shift, NumPy kuma yana ba da wasu ayyuka iri-iri na bitwise, gami da canjin dama, bitwise AND, bitwise OR, da bitwise XOR. Hakanan ana iya amfani da waษ—annan ayyuka zuwa bayanan lambobi, suna ba da sassauฦ™an kayan aiki don aiki tare da wakilcin binary.

A ฦ™arshe, da `NumPy hagu_shift()`Aiki kayan aiki ne mai ฦ™arfi don aiwatar da ayyukan motsi na hagu akan bayanan lambobi. Ta hanyar fahimtar haษ—in kai da amfani da shi, masu shirye-shiryen Python za su iya faษ—aษ—a tsarin fasahar su da magance rikitattun matsaloli na lambobi cikin sauฦ™i.

Shafi posts:

Leave a Comment