-
Notifications
You must be signed in to change notification settings - Fork 7
/
BitwiseOperations.sol
52 lines (47 loc) · 1.27 KB
/
BitwiseOperations.sol
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
52
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract BitwiseOperations {
// проверка бита по индексу
/**
* 00110010 & 00000010 = 00000010
* number = 50
* index = 0 == false
* index = 1 == true
* index = 2 == false
* ...
* index = 4 == true
*/
function isSetBit(uint8 number, uint8 index) external pure returns (bool) {
return number & (1 << index) != 0;
}
// установка бита в 1
/**
* 00110000 | 00000010 = 00110010
* number = 48
* index = 1
* result = 50
*/
function setBit(uint8 number, uint8 index) external pure returns (uint256) {
return number | (1 << index);
}
// установка бита в 0
/**
* 00110010 ^ 11111101 = 00110000
* number = 50
* index = 1
* result = 48
*/
function resetBit(uint8 number, uint8 index) external pure returns (uint256) {
return number & ~(1 << index);
}
// инверсия бита
/**
* 00110000 ^ 00000010 = 00110010
* number = 48
* index = 1
* result = 50
*/
function inverseBit(uint8 number, uint8 index) external pure returns (uint256) {
return number ^ (1 << index);
}
}