forked from pynchmeister/LTV-Blockchain-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitwiseOperators.sol
More file actions
51 lines (29 loc) · 1.17 KB
/
BitwiseOperators.sol
File metadata and controls
51 lines (29 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract BitwiseOperators {
// 5 in binary = 00000101
// 3 in binary = 00000011
/*
&, AND, Only keeps bits that are 1 in both
*/
// uint8 a = 5; // 00000101
// uint8 b = 3; // 00000011
// uint8 result = a & b; // 0000001 = 1
// | OR, Only keeps bits that are 1 in either
// uint8 a = 5; // 00000101
// uint8 b = 3; // 00000011
// uint8 result = a | b; // 00000111 = 7
// ^ XOR, Keeps bits that are 1 in one but not both
// uint8 a = 5; // 00000101
// uint8 b = 3; // 00000011
// uint8 result = a ^ b; // 00000110 = 6
// ~ NOT, Flips all bits (1 becomes 0 and vice versa)
// uint8 a = 5; // 00000101
// uint8 result = ~a; // 11111010 = 246
// << Left Shift, Moves bits to the left (multiplies by 2)
// uint8 a = 5; // 00000101
// uint8 result = a << 1; // 00001010 = 10
// >> Right Shift, Moves bits to the right (divides by 2)
uint8 a = 5; // 00000101
uint8 result = a >> 1; // 00000010 = 2
}