string_arithmetic.hpp
1: #ifndef STRING_ARITHMETIC_HPP
2: #define STRING_ARITHMETIC_HPP
3: /*------------------------------------------------------------------------------
4:
5: Author: Andy Rushton
6: Copyright: (c) Andy Rushton, 2004
7: License: BSD License, see ../docs/license.html
8:
9: Package of functions for performing bitwise logic and arithmetic on strings of 0, 1 and X values
10:
11: ------------------------------------------------------------------------------*/
12: #include "os_fixes.hpp"
13: #include "inf.hpp"
14: #include <string>
15:
16: ////////////////////////////////////////////////////////////////////////////////
17: // Logical operations
18: // I've had to name the bitwise_xxx and not logical_xxx to avoid name conflicts with STL
19: ////////////////////////////////////////////////////////////////////////////////
20:
21: // size changer
22:
23: std::string bitwise_resize(const std::string& argument, unsigned size = 0, char pad = '0');
24:
25: // comparisons
26:
27: bool bitwise_equality(const std::string& left, const std::string& right);
28: bool bitwise_inequality(const std::string& left, const std::string& right);
29:
30: // logical operations
31:
32: std::string bitwise_not(const std::string& argument);
33: std::string bitwise_and(const std::string& left, const std::string& right);
34: std::string bitwise_nand(const std::string& left, const std::string& right);
35: std::string bitwise_or(const std::string& left, const std::string& right);
36: std::string bitwise_nor(const std::string& left, const std::string& right);
37: std::string bitwise_xor(const std::string& left, const std::string& right);
38: std::string bitwise_xnor(const std::string& left, const std::string& right);
39:
40: // shift operations
41:
42: std::string bitwise_shift_left(const std::string& argument, unsigned shift);
43: std::string bitwise_shift_right(const std::string& argument, unsigned shift);
44: std::string bitwise_rotate_left(const std::string& argument, unsigned shift);
45: std::string bitwise_rotate_right(const std::string& argument, unsigned shift);
46:
47: ////////////////////////////////////////////////////////////////////////////////
48: // Unsigned arithmetic
49: ////////////////////////////////////////////////////////////////////////////////
50:
51: // size changers
52:
53: std::string unsigned_resize(const std::string& argument, unsigned size = 0);
54:
55: // comparisons
56:
57: bool unsigned_equality(const std::string& left, const std::string& right);
58: bool unsigned_inequality(const std::string& left, const std::string& right);
59: bool unsigned_less_than(const std::string& left, const std::string& right);
60: bool unsigned_less_than_or_equal(const std::string& left, const std::string& right);
61: bool unsigned_greater_than(const std::string& left, const std::string& right);
62: bool unsigned_greater_than_or_equal(const std::string& left, const std::string& right);
63:
64: // arithmetic operations
65:
66: std::string unsigned_add(const std::string& left, const std::string& right, unsigned size = 0);
67: std::string unsigned_subtract(const std::string& left, const std::string& right, unsigned size = 0);
68:
69: std::string unsigned_multiply(const std::string& left, const std::string& right, unsigned size = 0);
70: std::string unsigned_exponent(const std::string& left, const std::string& right, unsigned size = 0);
71: std::string unsigned_divide(const std::string& left, const std::string& right, unsigned size = 0);
72: std::string unsigned_modulus(const std::string& left, const std::string& right, unsigned size = 0);
73: std::string unsigned_remainder(const std::string& left, const std::string& right, unsigned size = 0);
74:
75: // logical operations
76:
77: std::string unsigned_not(const std::string& argument, unsigned size = 0);
78: std::string unsigned_and(const std::string& left, const std::string& right, unsigned size = 0);
79: std::string unsigned_nand(const std::string& left, const std::string& right, unsigned size = 0);
80: std::string unsigned_or(const std::string& left, const std::string& right, unsigned size = 0);
81: std::string unsigned_nor(const std::string& left, const std::string& right, unsigned size = 0);
82: std::string unsigned_xor(const std::string& left, const std::string& right, unsigned size = 0);
83: std::string unsigned_xnor(const std::string& left, const std::string& right, unsigned size = 0);
84:
85: // shift operations
86:
87: std::string unsigned_shift_left(const std::string& argument, unsigned shift, unsigned size = 0);
88: std::string unsigned_shift_right(const std::string& argument, unsigned shift, unsigned size = 0);
89:
90: // integer conversions
91:
92: unsigned long unsigned_to_ulong(const std::string& argument);
93: inf unsigned_to_inf(const std::string& argument);
94: std::string ulong_to_unsigned(unsigned long argument, unsigned size = 0);
95: std::string inf_to_unsigned(inf argument, unsigned size = 0);
96:
97: ////////////////////////////////////////////////////////////////////////////////
98: // Signed arithmetic
99: ////////////////////////////////////////////////////////////////////////////////
100:
101: // size changers
102:
103: std::string signed_resize(const std::string& argument, unsigned size = 0);
104:
105: // tests
106:
107: bool is_negative(const std::string& argument);
108: bool is_natural(const std::string& argument);
109: bool is_positive(const std::string& argument);
110: bool is_zero(const std::string& argument);
111:
112: // comparisons
113:
114: bool signed_equality(const std::string& left, const std::string& right);
115: bool signed_inequality(const std::string& left, const std::string& right);
116: bool signed_less_than(const std::string& left, const std::string& right);
117: bool signed_less_than_or_equal(const std::string& left, const std::string& right);
118: bool signed_greater_than(const std::string& left, const std::string& right);
119: bool signed_greater_than_or_equal(const std::string& left, const std::string& right);
120:
121: // arithmetic operations
122:
123: std::string signed_negate(const std::string& argument, unsigned size = 0);
124: std::string signed_abs(const std::string& argument, unsigned size = 0);
125: std::string signed_add(const std::string& left, const std::string& right, unsigned size = 0);
126: std::string signed_subtract(const std::string& left, const std::string& right, unsigned size = 0);
127:
128: std::string signed_multiply(const std::string& left, const std::string& right, unsigned size = 0);
129: std::string signed_exponent(const std::string& left, const std::string& right, unsigned size = 0);
130: std::string signed_divide(const std::string& left, const std::string& right, unsigned size = 0);
131: std::string signed_modulus(const std::string& left, const std::string& right, unsigned size = 0);
132: std::string signed_remainder(const std::string& left, const std::string& right, unsigned size = 0);
133:
134: // logical operations
135:
136: std::string signed_not(const std::string& argument, unsigned size = 0);
137: std::string signed_and(const std::string& left, const std::string& right, unsigned size = 0);
138: std::string signed_nand(const std::string& left, const std::string& right, unsigned size = 0);
139: std::string signed_or(const std::string& left, const std::string& right, unsigned size = 0);
140: std::string signed_nor(const std::string& left, const std::string& right, unsigned size = 0);
141: std::string signed_xor(const std::string& left, const std::string& right, unsigned size = 0);
142: std::string signed_xnor(const std::string& left, const std::string& right, unsigned size = 0);
143:
144: // shift operations
145:
146: std::string signed_shift_left(const std::string& argument, unsigned shift, unsigned size = 0);
147: std::string signed_shift_right(const std::string& argument, unsigned shift, unsigned size = 0);
148:
149: // integer conversions
150:
151: long signed_to_long(const std::string& argument);
152: inf signed_to_inf(const std::string& argument);
153: std::string long_to_signed(long argument, unsigned size = 0);
154: std::string inf_to_signed(inf argument, unsigned size = 0);
155:
156: ////////////////////////////////////////////////////////////////////////////////
157:
158: #endif