# Binary Logic

Provides the binary S3 class. The instance of binary is used to convert a decimal number (Base10) to a binary number (Base2). The Class provides some features e.G. shift(), rotate(), summary(). Based on logical vectors.

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

## Getting started

Starting with a simple conversion. Decimal (Base10) to binary (Base2) and vice versa.

## Operator

Behavior »Class Binary«

Operator Behavior
[== or !=] Comparision by value.
[<, <= or > , >=] Comparision by value.
[+ or -] Operations by value.
[*, ^] Operations by value.
[%/%, %%] Operations by value
[/] Not supported.
[&, ¦, xor] Bitwise Operations. The smaller vector is filled up with zeros.
[!] Indicates logical negation (NOT). Bitwise Operations

The logical == operator compares every element of the vector (Bitwise comparison). e.g.

The binary == operator compares the value and it does not distinguish between big and little endian.

BinaryLogic operators:

Operator Behavior
shiftLeft(binary), shiftRight(binary) shift Operation.
rotate(binary) shift Operation.
negate(binary) Indicates arithmetic negation. value <- value * (-1)
switchEndianess(binary)
bin2gray(binary) convert binary to gray code
gray2bin(gray) convert gray code to binary

## 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).

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.

• »Little Endian« : MSB at b and LSB at b.

• »Big Endian« : LSB at b and MSB at b.

##### 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.

other way around

other way around

## 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.

# Reference manual

install.packages("binaryLogic")

0.3.9 by Daniel Dörrhöfer, 2 years ago

https://github.com/d4ndo/binaryLogic

Report a bug at https://github.com/d4ndo/binaryLogic/issues

Browse source code at https://github.com/cran/binaryLogic

Authors: Daniel Dörrhöfer [aut, cre] , Aaron Rudkin [ctb]

Documentation:   PDF Manual