Skip to content

Commit b2fe5da

Browse files
committed
feat: integer to binary array conversion
1 parent 4a02bfc commit b2fe5da

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

api/Binary.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
binary.cpp - Definitions for binary constants
3+
Copyright (c) 2025 jsnkan. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "Binary.h"
21+
22+
void intToBinaryArray(unsigned char n, int *arr) {
23+
if (arr == 0) return;
24+
for (int i = 7; i >= 0; i--) {
25+
arr[i] = n & 1;
26+
n >>= 1;
27+
}
28+
}
29+
30+
unsigned char binaryArrayToInt(int *arr) {
31+
if (arr == 0) return 0;
32+
unsigned char n = 0;
33+
for (int i = 0; i < 8; i++) {
34+
n = (n << 1) | (arr[i] & 1);
35+
}
36+
return n;
37+
}

api/Binary.h

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#define DEPRECATED(x)
3535
#endif
3636

37+
void intToBinaryArray(unsigned char n, int *arr);
38+
unsigned char binaryArrayToInt(int *arr);
39+
3740
enum {
3841
B0 DEPRECATED(0b0 ) = 0,
3942
B00 DEPRECATED(0b00 ) = 0,

0 commit comments

Comments
 (0)