-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME.Rmd
More file actions
231 lines (153 loc) · 5.26 KB
/
README.Rmd
File metadata and controls
231 lines (153 loc) · 5.26 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
output:
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
binaryLogic
===========
[](https://travis-ci.org/d4ndo/binaryLogic)
[](https://www.r-pkg.org/pkg/binaryLogic)
[](https://cran.r-project.org/package=binaryLogic)
Binary Logic GNU R Package
Convert, negate, shift and rotate binary digits.
(switchEndianess, bin2gray, bytesNeeded, binaryPrefix, fillUpToByte).
## Installation
devtools are required to install "binaryLogic" from github: [devtools](https://github.com/hadley/devtools)
```R
library(devtools)
# install 'binaryLogic'
install_github("d4ndo/binaryLogic")
library(binaryLogic)
```
Getting started
---------------
Starting with a simple conversion. Decimal (Base10) to binary (Base2) and vice versa.
```{r, echo=FALSE}
library(binaryLogic)
```
```{r, echo=TRUE}
the_answer_to_the_ultimate_question_of_life_the_universe_and_everything <- as.binary(42)
the_answer_to_the_ultimate_question_of_life_the_universe_and_everything
as.numeric(the_answer_to_the_ultimate_question_of_life_the_universe_and_everything)
summary(the_answer_to_the_ultimate_question_of_life_the_universe_and_everything)
```
Operator
---------
Behavior »Class Binary«
```{r, echo=FALSE, message=FALSE}
library(knitr)
source("operator.R")
```
```{r, echo=FALSE, message=TRUE}
kable(op)
```
The logical == operator compares every element of the vector (Bitwise comparison). e.g.
```{r, echo=TRUE}
two <- as.binary(2); two <- as.logical(two); two == two;
```
The binary == operator compares the value and it does not distinguish between big and little endian.
```{r, echo=TRUE}
two <- as.binary(2); two == two;
```
BinaryLogic operators:
```{r, echo=FALSE, message=TRUE}
kable(op2)
```
Information
-----------
This class is just not that great at heavy number crunching, but it brings some benefits. Especially if you like to work using vectors in R. The »binary« class inherits from the »logical« class. Some function from package ``binaryLogic`` can be applied to logical vectors such as shift or rotate (see help).
The internal structure looks like this. It is composed of a »logical vector« and several attributes. In this example(Big-Endian), it corresponds to the value = 2(Base10).
```{r, echo=TRUE}
structure(c(TRUE, FALSE), class = c("binary", "logical"), signed = FALSE, littleEndian = FALSE)
```
The binary number is represented by a logical vector. The Bit order usually follows the same endianess as the byte order. How to read:
* Little Endian (LSB) —> (MSB)
* Big Endian (MSB) <— (LSB)
The Big Endian endianess stores its MSB at the lowest adress.
The Little Endian endianess stores its MSB at the highest adress.
e.g.
```{r, echo=TRUE}
b <-binary(8)
b
```
* »Little Endian« : MSB at b[1] and LSB at b[8].
* »Big Endian« : LSB at b[1] and MSB at b[8].
##### Signed digit:
Calculation:
The size has to be considerd in a calculation with a signed number.
e.G.: An 8 Bit signed number can hold this range of base10 numbers [127 to -128]. You will run into a problem if a calculation is outside of this range.
The reference is the larger number in size.
Size:
The size »must« be specified. In Byte e.G. (1 Byte = 8 Bit, 2 Byte, .. n Byte). By default it is 2 Byte.
##### Unsigned digit:
Calculation:
An unsigned number will increase it's size when caluculation is not in range.
Size:
The size »can« be specified. In Bit. (1 Bit , 2 Bit, .. n Bit). There is no default size because the size is calculated on the fly.
More Converting
---------------
### Integer
```{r, echo=TRUE}
as.binary(0xAF)
as.binary(42)
as.binary(42, littleEndian=TRUE)
as.binary(c(0xAF, 0xBF, 0xFF))
as.binary(c(2,4,8,16), signed=TRUE, size=1)
as.binary(-1, signed=TRUE, size=1)
```
other way around
```{r, echo=TRUE}
two <- as.binary(2, signed=TRUE, size=4)
as.integer(negate(two))
# or
as.double(two)
# alias for
as.numeric(two)
```
### Logical
```{r, echo=TRUE}
as.binary(c(1,1,0), signed=TRUE, logic=TRUE)
as.binary(c(TRUE,TRUE,FALSE), logic=TRUE)
bigEndian <- as.binary(c(1,1,0,0), logic=TRUE)
summary(bigEndian)
littleEndian <- switchEndianess(bigEndian)
print(littleEndian)
littleEndian <- as.binary(bigEndian, littleEndian=TRUE)
print(littleEndian)
summary(littleEndian)
```
other way around
```{r, echo=TRUE}
as.logical(as.binary(2))
```
### Raw
```{r, echo=TRUE}
b <- as.binary(charToRaw("A"))
summary(b)
as.raw(b)
```
### Gray code
```{r, echo=TRUE}
b <- as.binary(0:7, n=3)
g <- lapply(b, bin2gray)
print(g)
gray2bin(g[[8]])
```
Special Case
------------
Be aware about this kind of notation »0xAF«. Because Gnu R converts this to an integer first and then it will be converted to a binary digit. This is just a limitation, if you want to use a little endian formation. It can be fixed by using switchEndianess setting the stickyBits=TRUE.
```{r, echo=TRUE}
#Watch out for this notation
as.binary(0xAF, littleEndian=TRUE)
littleEndian <- switchEndianess(as.binary(0xAF), stickyBits = TRUE)
littleEndian
summary(littleEndian)
```