calculator.org

the calculator home page

Base-n, hex, octal and binary modes


For calculations involving binary,

octal or hexadecimal arithmetic, special modes are used. The calculator is shown in hexadecimal mode.

These modes support integer arithmetic only. You can quickly change between number bases using the last row of buttons on the function keypad.

The current number base is indicated within the display area.

Two's complement representation

By default, signed numbers are displayed in exactly

the same way as in decimal mode, with a minus sign. In computing and logic

design it is more common to work with two's

complement numbers. To enable two's complement number display use the

Option/Display dialog.

The two's complement drop-down list allows you to select the word length.

Once this is selected, values will be truncated to the selected number of bits

for display, with leading zeroes added.

The two's complement setting only affects the displayed value. For example,

if you keep adding positive values until the value "rolls over", the actual

accumulated positive number will be stored internally, although the display will

show the truncated value. However, if you switch to decimal mode, or disable

two's complement, you will see a large positive number, rather than a smaller

negative one. For example, if an 8-bit word length is selected and the decimal

number -2 is entered, the value FE will be shown when you convert to

hexadecimal. If a decimal of 254 is entered and converted to hexadecimal this

will also show FE, but internally this is a different number. The two's

complement fixed word length gives you an 8-bit "window" onto the underlying

value.

Entering values in various number bases

In octal and binary mode some of the buttons are disabled. The button layout

for binary mode is shown below, with two's complement and 16-bit word length:

For base-n numbers you need to set the number base in Options/General. This can be any number base between 2 and 36, and the

digits are represented in the range 0-Z. Once the number base is set, it will be

remembered whenever Base-n is selected. Setting the number base to ten is useful

if you need to use some of the number base or bit-wise functions while working

with decimal integers.

For number bases up to 16 (hexadecimal) the 0-9 and A-F buttons gradually

become available as required. Beyond this you need to use the keyboard to input

digits. The keyboard shortcuts for digits are the capital letters.

Base 36 Example

Using Options/Mode, select mode Base-n and set the number base to 22 (you

could also set it to a higher number).


Clear the calculator and use the buttons to enter C A

:


CA

Now use the keyboard to enter L (remembering to use the keyboard shift key).


CAL

Using the buttons again, enter C 9 5

:


CALC95

Now do some arithmetic in the normal way, e.g. + 3 =

:


CALC98

Logical operators

Logical AND,

OR, XOR and XNOR

operators are available. They take two operands. There are also unary operators

to negate and a NOT operator.

Bit-wise shift operators

Pressing the shift button converts the shift

left button <<

into right shift >> . The shift operator takes two operands - the

number to be shifted and the number of bits to be shifted by. It is sometimes

inconvenient to specify the number of shift bits in the same number system as

the number to be shifted, and you can change number mode during the course of

the calculation without affecting the result.

Right shifting by a negative number is

equivalent to left shifting and

vice versa.

IP Address Mode

In IP Address mode, a 32-bit number is represented in a byte-wise notation

where each byte is shown as a decimal number separated by periods. The numbers

for each byte can range from 0 to 255, except in Hollywood films where

impossibly large byte numbers can be seen (e.g. 23.75.345.200 in the film "The Net"!). So a typical IP address would be 213.143.15.48

(the IP address of www.calculator.org current at the time of writing). This number could also

be represented as a hexadecimal four byte number: D58F0F30. It is easy to do

such conversions by switching between IP Address mode and Hexadecimal mode.

Representing the numbers in this way is a convenience because different parts

of the address have special meaning depending on the "class" of address. The

classes are defined as class A, class B or class C depending on which part of

the address is used to define the network address. With the exception of very

large organisations and ISPs (Internet Service Providers) most networks use

class C addresses where the network address is defined by the first three bytes

and the last byte is assigned to each computer as required. The subnet mask is

therefore 255.255.255.0. Class A, B and C subnet masks can be called up using

the A , B and C buttons. To obtain the network address for a

class C address, for example, you would enter the address and then AND it with

the class C mask:

1.2.3.4 AND C Result: 1.2.3.0