1# Bitwise
2## Terminal based bitwise calculator in curses
3[![Build Status](https://travis-ci.org/mellowcandle/bitwise.svg?branch=master)](https://travis-ci.org/mellowcandle/bitwise)[![codecov](https://codecov.io/gh/mellowcandle/bitwise/branch/master/graph/badge.svg)](https://codecov.io/gh/mellowcandle/bitwise)
4[![Snap Status](https://build.snapcraft.io/badge/mellowcandle/bitwise.svg)](https://build.snapcraft.io/user/mellowcandle/bitwise)<a href="https://scan.coverity.com/projects/mellowcandle-bitwise">
5  <img alt="Coverity Scan Build Status"
6       src="https://img.shields.io/coverity/scan/18170.svg"/>
7<a href="https://repology.org/project/bitwise/versions">
8    <img src="https://repology.org/badge/vertical-allrepos/bitwise.svg" alt="Packaging status" align="right">
9</a>
10
11_Bitwise_ is multi base interactive calculator supporting dynamic base conversion and bit manipulation.
12It's a handy tool for low level hackers, kernel developers and device drivers developers.
13
14Some of the features include:
15* Interactive ncurses interface
16* Command line calculator supporting all bitwise operations.
17* Individual bit manipulator.
18* Bitwise operations such as NOT, OR, AND, XOR, and shifts.
19
20##
21
22![Demo](https://github.com/mellowcandle/bitwise/raw/master/resources/bitwise.gif "Bitwise demo2")
23
24## Usage
25_bitwise_ can be used both Interactively and in command line mode.
26
27### Command line calculator mode
28In command line mode, bitwise will calculate the given expression and will output the result in all bases including binary representation.
29
30_bitwise_ detects the base by the preface of the input (_0x/0X_ for hexadecimal, leading _0_ for octal, _b_ for binary, and the rest is decimal).
31
32**NEW** Bitwise now support parsing IPv4 addresses, it will also output the possible IPv4 address in both Network and reveresed byte order.
33
34### Examples:
35
36#### Simple base conversion
37
38![conversion](https://github.com/mellowcandle/bitwise/raw/master/resources/cmdline.png "Bitwise conversion")
39
40
41#### C style syntax Calculator
42
43![calculator](https://github.com/mellowcandle/bitwise/raw/master/resources/conversion.png "Bitwise calculator")
44
45### Interactive mode
46_bitwise_ starts in interactive mode if no command line parameters are passed or if the _-i | --interactive_ flag is passed.
47In this mode, you can input a number and manipulate it and see the other bases change dynamically.
48It also allows changing individual bits in the binary.
49You can show the help screen by pressing <kbd> F1 </kbd>.
50
51#### Navigation in interactive mode
52To move around use the arrow keys, or use _vi_ key bindings : <kbd> h </kbd> <kbd> j </kbd> <kbd> k </kbd> <kbd> l </kbd>.
53Leave the program by pressing <kbd> q </kbd>.
54
55##### Binary specific movement
56You can toggle a bit bit using the <kbd> space </kbd> key.
57You can jump a byte forward using <kbd> w </kbd> and backwards one byte using <kbd> b </kbd>.
58
59#### Bitwise operation in interactive mode
60
61##### Setting the bit width:
62
63Reducing or extending the bit width interactively is also very easy, just use:
64<kbd> ! </kbd> for 8bit, <kbd> @ </kbd>  for 16Bit, <kbd> $ </kbd> for 32Bit and <kbd> * </kbd> for 64Bit.
65When changing the bit width, the number is *masked* with the new width, so you might lost precision, use with care.
66
67##### NOT:
68
69Press <kbd> ~ </kbd> to perform the NOT operator.
70
71##### Shifts
72
73Press <kbd> < </kbd> and <kbd> > </kbd> to perform the left or right shift.
74
75#### expression calculator in interactive mode
76
77You can enter expression calculator mode by typing <kbd> : </kbd> (Just like in vim).
78
79To exit the mode, just press <kbd> ESC </kbd>.
80
81In this mode, you can type any expression you like to be evaluated.
82The result will be printed in the history window and also printed in the binary and various bases on top.
83
84###### operators and functions
85* All C operators are supported, additionally, you can use the "$" symbol to refer to the last result.
86* Refer to a specific bit by using the function _BIT(x)_.
87
88###### commands
89* _help_ - Show the help screen.
90* _clear_ - Clear the history window.
91* _width_ [8 | 16 | 32 | 64] - Set the required width mask
92* _output_ [decimal | hex | octal | binary | all] - Set the default output for results.
93* _q_ - Exit
94
95## Integration with other software
96### Vim
97* [vim-bitwise](https://github.com/mellowcandle/vim-bitwise "vim bitwise")
98
99## Installation
100
101### Linux
102#### Ubuntu
103From 20.04 you can just type
104```
105sudo apt-get install bitwise
106```
107For earlier versions:
108```
109sudo add-apt-repository ppa:ramon-fried/bitwise
110sudo apt-get update
111sudo apt-get install bitwise
112```
113#### Snap
114If your distribution supports Snap just type:
115`
116sudo snap install bitwise
117`
118#### OpenSuse
119`
120zypper install bitwise
121`
122
123#### Arch
124You can use the AUR repository: https://aur.archlinux.org/packages/bitwise/
125
126#### Void
127_bitwise_ is in the default repository, so just type:
128`
129sudo xbps-install -S bitwise
130`
131
132#### Buildroot / Yocto
133Bitwise is available both in Buildroot and in Yocto, please refer to the documentation on how to add those to your target image.
134
135### macOS
136
137#### MacPorts
138```
139sudo port install bitwise
140```
141
142#### Homebrew
143```
144brew install bitwise
145```
146
147### Windows
148NCurses doesn't support windows. You can use the windows subsystem for Linux as a workaround.
149
150### Nix
151```
152nix-env -i bitwise
153```
154
155### Building from source
156
157#### Prerequisites
158* libreadline
159* libncurses (with forms)
160* libcunit (only needed for testing)
161
162On Ubuntu/Debian system you can just paste:
163```
164sudo apt-get install build-essential
165sudo apt-get install libncurses5-dev
166sudo apt-get install libreadline-dev
167sudo apt-get install libcunit1-dev
168```
169On Mac systems:
170```
171brew install automake
172brew install autoconf
173brew install readline
174export LDFLAGS="-L/usr/local/opt/readline/lib"
175export CPPFLAGS="-I/usr/local/opt/readline/include"
176```
177- Download [the latest release](https://github.com/mellowcandle/bitwise/releases/latest)
178
179```sh
180tar xfz RELEASE-FILE.TAR.GZ
181cd RELEASE-DIR
182./configure
183make
184sudo make install
185```
186
187Running unit tests by typing
188``` make check ```
189
190### Contribution
191* Instal prerequisites
192* Fork the repo
193* Run ```./bootstrap.sh```
194* Follow the building from source section.
195* commit and send pull request
196