Skip to content

Commit

Permalink
Merge pull request #11 from ajitonelsonn/dev
Browse files Browse the repository at this point in the history
operasaun bitwise
  • Loading branch information
ajitonelsonn authored Apr 4, 2024
2 parents f923e50 + 57366da commit e9d9180
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
66 changes: 66 additions & 0 deletions operasaun bitwise/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Operasaun bitwise

Operasaun bitwise iha Python permite ita halo operasaun aaritmétika iha nível bit. Iha Python, ita bele uza operadores bitwise hanesan `&` (AND bit-a-bit), `|` (OR bit-a-bit), `^` (XOR bit-a-bit), `~` (NOT bit-a-bit), `<<` (Shift left), no `>>` (Shift right) atu manipula valores iha nível bit.

Lista operadores bitwise sira:

1. `&`: AND bit-a-bit.
2. `|`: OR bit-a-bit.
3. `^`: XOR bit-a-bit.
4. `~`: NOT bit-a-bit.
5. `<<`: Shift left.
6. `>>`: Shift right.

Ezemplu sira:

```python
# AND bit-a-bit
print(5 & 3) # Sai 1 (101 & 011 = 001)

# OR bit-a-bit
print(5 | 3) # Sai 7 (101 | 011 = 111)

# XOR bit-a-bit
print(5 ^ 3) # Sai 6 (101 ^ 011 = 110)

# NOT bit-a-bit
print(~5) # Sai -6 (~101 = -110)

# Shift left
print(5 << 1) # Sai 10 (101 << 1 = 1010)

# Shift right
print(5 >> 1) # Sai 2 (101 >> 1 = 10)
```

### BITWISE

<img src='https://miro.medium.com/v2/resize:fit:1088/format:webp/1*xIxbMeCIPufBSCAEIKAG9A.png'>

### EZAMPLE

#### AND

<img src='https://journaldev.nyc3.cdn.digitaloceanspaces.com/2019/02/python-bitwise-and-operator.png'>

#### OR

<img src='https://journaldev.nyc3.cdn.digitaloceanspaces.com/2019/02/python-bitwise-or-operator.png'>

#### XOR

<img src='https://journaldev.nyc3.cdn.digitaloceanspaces.com/2019/02/python-bitwise-xor-operator.png'>

#### Bitwise Ones’ Complement Operator

<img src='https://journaldev.nyc3.cdn.digitaloceanspaces.com/2019/02/python-bitwise-ones-complement-operator.png'>

#### Shift Left

<img src='https://journaldev.nyc3.cdn.digitaloceanspaces.com/2019/02/python-bitwise-left-shift-operator.png'>

#### Shift Right

<img src='https://journaldev.nyc3.cdn.digitaloceanspaces.com/2019/02/python-bitwise-right-shift-operator.png'>

Referencia : https://www.digitalocean.com/community/tutorials/python-bitwise-operators
49 changes: 49 additions & 0 deletions operasaun bitwise/exerciciu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
### Exerciciu I

Klaru! Hau sei fó imi alguns exercísiu sira hodi pratika operadores bitwise iha Python:

1. **Exercísiu 1**: Kalkula AND bit-a-bit entre númeru 10 no 7.

```python
resultado = 10 & 7
print("AND bit-a-bit entre 10 no 7:", resultado)
```

2. **Exercísiu 2**: Kalkula OR bit-a-bit entre númeru 10 no 7.

```python
resultado = 10 | 7
print("OR bit-a-bit entre 10 no 7:", resultado)
```

3. **Exercísiu 3**: Kalkula XOR bit-a-bit entre númeru 10 no 7.

```python
resultado = 10 ^ 7
print("XOR bit-a-bit entre 10 no 7:", resultado)
```

4. **Exercísiu 4**: Kalkula NOT bit-a-bit ba númeru 10.

```python
resultado = ~10
print("NOT bit-a-bit ba 10:", resultado)
```

5. **Exercísiu 5**: Shift left 2 posisaun ba númeru 10.

```python
resultado = 10 << 2
print("Shift left 2 posisaun ba 10:", resultado)
```

6. **Exercísiu 6**: Shift right 2 posisaun ba númeru 10.

```python
resultado = 10 >> 2
print("Shift right 2 posisaun ba 10:", resultado)
```

```python
print(format(5 ^ 3, '08b'))
```
19 changes: 19 additions & 0 deletions operasaun bitwise/kode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# AND bit-a-bit
print(5 & 3) # Sai 1 (101 & 011 = 001)
print(format(5 & 3, '08b'))

# OR bit-a-bit
print(5 | 3) # Sai 7 (101 | 011 = 111)
print(format(5 | 3, '08b'))
# XOR bit-a-bit
print(5 ^ 3) # Sai 6 (101 ^ 011 = 110)
print(format(5 ^ 3, '08b'))
# NOT bit-a-bit
print(~5) # Sai -6 (~101 = -110)
print(format(~5, '08b'))
# Shift left
print(5 << 1) # Sai 10 (101 << 1 = 1010)
print(format(5 << 1, '08b'))
# Shift right
print(5 >> 1) # Sai 2 (101 >> 1 = 10)
print(format(5 >> 1, '08b'))

0 comments on commit e9d9180

Please sign in to comment.