# Hex, Binary and bitwise operations

LARD allows you to write binary, octal and hex constants using the following syntax:

```	2#01010101
8#172
16#8E92
```
For completeness you can also prefix decimal constants with 10# if you wish.

Here's an example that shows how an integer value can be broken down into subfields using hex constants and the bitwise logical operators:

```	opcode:=instr and 16#00F;
reg1:=(instr and 16#0F0) >> 4;
reg2:=(instr and 16#F00) >> 8;
```
The bitwise logical operators are and, or, xor and not. Note that &&, || and ! are boolean operators not bitwise operators.

Shifts are also available using << and >>.

You can output hex values using the printhex function:

```	printhex(instr);
print("\nopcode=");
printhex(instr and 16#00F);
print('\n');
```
Values output from printhex are zero-extended in an 8 character field.

Note that internally integers are normally treated as signed but printhex always prints them unsigned, so for example

```	printhex(16#80000000 >> 4);
```
will output F8000000. Values read from files can also use the base # syntax to specify their base. Normally if no base is specified in this way the input is parsed as decimal. However it is possible to override this default using the set_default_input_base function.

To illustrate this, here is a function that initialises an array with hex values read from a file.

```	init_array_from_file(v:var(int^(n:val(int))),
fn:val(string)):expr(void)=(
a:var(int).
f:var(tfile).

newstringvalparam(fn);
set_default_input_base(16);  /* file contains hex data */
f:=topenin(fn);
forseq a in 0 to (n-1) do (
v[a]:=freadint(f)
);
close(f);
endstringvalparam(fn)
).
```