Problems and Solutions for Bit and String Manipulations
Problems and Solutions for Bit and String Manipulations
for
Bit and String Manipulations
by
Willi-Hans Steeb
International School for Scientific Computing
at
University of Johannesburg, South Africa
Yorick Hardy
Department of Mathematical Sciences
at
University of South Africa, South Africa
Preface
The material was tested in our lectures given around the world.
http://issc.uj.ac.za
v
Contents
3 Binary Matrices 58
6 Cellular Automata 76
7 String Manipulations 78
Bibliography 82
Index 83
vi
Chapter 1
1.1 Introduction
Bit is short for binary digit with either of the two digits 0 and 1 in the
binary number system. The bit is the smallest unit of storage in a binary
system. Binary refers to base 2 arithmetic using the digits 0 and 1. Thus
a bit is a binary digit (i.e. a digit in the binary number system). It is the
most basic unit of information in digital computing.
A boolean function is a function f with domain {0, 1}n and range {0, 1}, for
some positive integer n. Here {0, 1}n denotes the n-fold Cartesian product
of the set {0, 1} with itself, that is, the set of binary n-tuples. Thus we also
write
f : {0, 1}n → {0, 1}.
n
There are 22 boolean function of n variables.
1
2 Problems and Solutions
Notation. In the text we use for the AND-operation ·, we use for the
OR-operation +, we use for the XOR-operation ⊕ and we use for the NOT-
operation x.
The shift operations allow bits to be moved to the left or right in a word.
There are three types of shift operations: logical, rotate and arithmetic.
The logical shift moves bits to the left or right. The bits which fall off
the end of the word are discarded and the word is filled with 0’s from the
opposite end. For example a logical right shift of the 8 bit binary number
1000 1011 provides 0100 0101. Shift instructions include a repeat value,
which is the number of times the single bit shift operation is repeated. A
rotate operation (not implemented in C++, C, Java, C# and Perl) is a
circular shift in which no bits are discarded. An arithmetic right shift is
similar to a logical right shift except that the leftmost bits are filled with
the sign bit of the original number instead of 0’s. For example, an arith-
metic right shift of the 8 bit number 1000 1011 provides 1100 0101. >>
is the right shift operation in C++, C, Java, C# and Perl. << is the left
shift operation in these languages. In C++ and C they can be applied to
integral operands, that is char, short, int, and long, whether signed or
unsigned. The size of char is 8 bits, the size of short is 16 bits, and the
size of int and long is 32 bits. The sign bit is at the left most position, for
example for int it is at position 31 (counting from 0).
Hex notation is indicated in C, C++, Java and Perl as 0x. For example
0xFF is the number 255 (base 10).
Basic Bitwise Operations 3
1.2 Quickies
Problem 1. Let x ∈ {0, 1}.
(i) Find x · x, x ⊕ x, x + x.
(ii) Find x · x̄, x ⊕ x̄, x + x̄.
(iii) Find x · 0, x · 1, x ⊕ 0, x ⊕ 1, x + 0, x + 1.
x · y = x + y.
x ⊕ y = x · y.
x ⊕ y = x + y.
(x1 + x2 ) · (x3 + x4 ) = x1 · x3 + x1 · x4 + x2 · x3 + x2 · x4 ?
(ii) Is
(x1 + x2 ) ⊕ (x3 + x4 ) = x1 ⊕ x3 + x1 ⊕ x4 + x2 ⊕ x3 + x2 ⊕ x4 ?
(x1 · x2 ) ⊕ x3 = x1 · (x2 ⊕ x3 ) ?
f (x, y) = x + (x · y)
Basic Bitwise Operations 5
f (x, y) = x · (x + y)
(x · y) + (y · z) + (z · x) = (x · y) ⊕ (y · z) ⊕ (z · x)
Problem 8. Let a, b, c ∈ { 0, 1 }. Is
(a ⊕ b) · c = a ⊕ (b · c) ?
a ⊕ b = x · y.
(x ⊕ y) ⊕ z = x ⊕ (y ⊕ z) ?
(ii) Let x and y be arbitrary bitstrings of the same length. Let ⊕ be the
XOR operation. Calculate
(x ⊕ y) ⊕ y.
(x ⊕ y) · z = (x · z) ⊕ (y · z) ?
b ⊕ (a · c)
6 Problems and Solutions
Find the boolean expression (sum of products) for this truth table. Can the
expression be simplified?
Problem 15. Given k-bit inputs and m-bit outputs. How many boolean
function are there?
E =x·z+x·y+x·y+y·z
be simplified?
Problem 19. The truth table of a full adder with inputs a, b, cin and
outputs y, cout is
Basic Bitwise Operations 7
a b cin y cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Find the Boolean expression for this truth table.
Problem 20. A 1-bit full adder is a circuit with three 1-bit inputs (the
bits to be added) and two 1-bit outputs (the sum and the carry). It is given
by
sum = (a ⊕ b) ⊕ c
carry = a · b + a · c + b · c = a · b + (a + b) · c = a · b + (a ⊕ b) · c.
Find the thruth table.
Problem 21. We define the binary dot product of two bitstrings x and y
of the same length n as
x · y := (x1 y1 + x2 y2 + · · · + xn yn ) mod 2.
Let x, y, z be bitstrings of the same length. Verify that
(x ⊕ y) · z ≡ (x · z) ⊕ (y · z).
Problem 23. Prove the following statements by (i) using a truth table,
(ii) using the properties of boolean algebra (algebraic proof).
(a) 1 + a ≡ 1.
(b) a ⊕ b ≡ a ⊕ b
(c) (a · b) ⊕ (a · b) ≡ a · (b ⊕ b).
(a) Determine |Y (x)|, where | | denotes the cardinality, i.e. the number of
elements.
(b) Determine |C(x, y)|, where | | denotes the cardinality, i.e. the number
of elements.
(c) Let (x, y). How many unique pairs are (x, y) in C(x, y)?
(d) Which group properties hold for Y (x0 ) where x0 = (0, 0, . . . , 0)?
Problem 26. Show that the XOR-gate can be built from 4 NAND-gates.
Show that the AND-gate can be built from 2 NAND-gates. Show that the
OR-gate can be built from 3 NAND-gates. Show that the NOR-gate can
be built from 4 NAND-gates.
Problem 28. Find the truth table for the boolean function
f (x1 , x2 , x3 ) = x1 ⊕ x2 ⊕ x3 .
Problem 30. Show that every boolean function f : {0, 1}n → {0, 1} can
be expanded as follows
Problem 31. Apply the expansion theorem given at the previous exercise
repeatedly to each variable of
f (x1 , x2 , x3 ) = x1 · x2 + x2 · x3
and
1 if xk ≥ 0.5
sk =
0 if xk < 0.5
where x0 ∈ [0, 1]. We call s = s0 s1 s2 . . . the output symbol. Show that if
x0 ∈ [0.78125, 0.8125] then the output coincide for the first three bits.
Problem 35. The D-type latch works as follows. When the control signal
latch-enable (LEN ) is high, the latch is in the transparent mode and the
input signal D̄ is available at the output. When the LEN signal is low, the
input data is latched to the output and is retained until LEN goes back to
high.
(i) Give the truth table from this description.
(ii) Give the boolean equation for this latch.
(iii) Give the circuit.
Problem 36. Consider two unsigned int (32 bits) n and n + 1. What
is the condition that the parity of n and n + 1 are the same? For example
1 and 2 have the same parity but 2 and 3 not.
10 Problems and Solutions
f (a, b, c, d) = a · d + (a · c + b) · (c · d + e).
Find the truth table. Discuss. Write a C++ program that generates the
truth table.
f (x, y, z) = (x · ȳ) + x · z) + x̄
f (a, b, c) = a · b · c̄ + ā · c + ā · b̄.
f (x, y, z) = x · (x̄ + y) + y · (y + z) + y.
Problem 43. Let a, b ∈ {0, 1}. Find all solutions of the equation a ⊕ b =
a · b, where ⊕ denotes the XOR-operation and · the AND-operation.
Basic Bitwise Operations 11
Problem 44. Let a, b, c, d ∈ {0, 1}. Find all solutions of the equation
a · b = c ⊕ d, where ⊕ denotes the XOR-operation and · the AND-operation.
i1 ⊕ i2 ⊕ i3 = 0
o1 ⊕ o2 ⊕ o3 ⊕ (i1 ∨ i2 ∨ i3 ) = 1.
Find all solutions of this system of boolean equation. Here ⊕ denotes the
XOR-operation and ∨ denotes the OR-operation.
Problem 46. After the ASCII table the capital A is identified with the
integer 65 (base 10) and the small a is identified with the integer 97 (base
10). Write down these two numbers in binary (8 bits) and apply the XOR-
operation. Discuss.
Problem 47. Prove (i) using a truth table, (ii) using the properties of
boolean algebra (algebraic proof)
(a) 1 + x = 1
(b) x̄ ⊕ ȳ = x ⊕ y
(c) (x · ȳ) ⊕ x = x · y
(d) that ⊕ is associative.
a · (b · c) = x + (y + z).
12 Problems and Solutions
int main(void)
{
int x = 17;
int r = x & (-x);
cout << "r = " << r << endl;
x = 101;
r = x & (-x);
cout << "r = " << r << endl;
x = -5;
r = x & (-x);
cout << "r = " << r << endl;
return 0;
}
int main(void)
{
unsigned int i = 10; unsigned int j = 11;
unsigned int k = 12; unsigned int l = 13;
unsigned r = ((i^j) & (k*l)) | ((i^k) & (j^l));
cout << "r = " << r << endl;
return 0;
}
Problem 52. In the following C++ program the bitwise XOR ^ is used.
What is the output?
// branch.cpp
#include <iostream>
using namespace std;
Basic Bitwise Operations 13
int main(void)
{
unsigned int a = 10; unsigned int b = 9;
unsigned int x = 10;
x = a^b^x;
cout << "x = " << x << endl;
a = 7; b = 9; x = 9;
x = a^b^x;
cout << "x = " << x << endl;
return 0;
}
Problem 53. In the following C++ program we apply the NOT operation
~ and the OR operation |. What is the output?
// deMorgan.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int x = 8345; unsigned int y = 34512;
unsigned int r1 = x & y;
unsigned int r2 = ~(~x | ~y);
if(r1==r2) cout << "true"; else cout << "false";
return 0;
}
Problem 54. What is output of the following C++ program using the
AND and NOT operations
// clearbit.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i = 476; unsigned int j = 677;
unsigned int r = i & ~j;
cout << "r = " << r << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i=1; unsigned int j=2;
unsigned int k=3;
unsigned int f1 = (((i) & (j)) | ((~i) & (k)));
unsigned int f2 = (((i) & (k)) | ((j) & (~k)));
unsigned int f3 = ((i) ^ (j) ^ (k));
unsigned int f4 = ((j) ^ ((i) | (~k)));
cout << "f1 = " << f1 << endl;
cout << "f2 = " << f2 << endl;
cout << "f3 = " << f3 << endl;
cout << "f4 = " << f4 << endl;
return 0;
}
Problem 56. The following C++ program uses the AND operation.
What is the output?
// maxmin.cpp
#include <iostream>
using namespace std;
int main(void)
{
int x = 22; int y = 18;
int t = ((x-y) & -(x < y));
int r1 = y + t;
cout << "r1 = " << r1 << endl;
int r2 = x-t;
cout << "r2 = " << r2 << endl;
return 0;
}
Problem 57. The following C++ uses the AND operation. Note that !
is the logical NOT. What is the output?
// parity1.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int v = 19;
bool p = false;
Basic Bitwise Operations 15
Problem 58. The following C++ code uses the AND-operation. Here !
is the logical NOT. What is the output?
// power.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i1 = 2345678;
bool b1 = !(i1 & (i1-1)) && (i1 > 0);
cout << "b1 = " << b1 << endl;
unsigned int i2 = 65536;
bool b2 = !(i2 & (i2-1)) && (i2 > 0);
cout << "b2 = " << b2 << endl;
unsigned int i3 = 0;
bool b3 = !(i3 & (i3-1)) && (i3 > 0);
cout << "b3 = " << b3 << endl;
return 0;
}
Problem 59. The following C++ code use the NOT and AND-operation.
What is the output?
// bitstozero.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int j = 4236571;
unsigned int r = j & ~0xFF;
cout << "r = " << r << endl;
return 0;
}
Problem 60. In the following C++ program we use the XOR and NOT-
operation. What is the output?
// XORNOT.cpp
16 Problems and Solutions
#include <iostream>
using namespace std;
int main(void)
{
unsigned int j;
unsigned int r = j ^ (~j);
cout << "r = " << r << endl;
return 0;
}
int main(void)
{
unsigned int v = 19;
unsigned int r = v | (v + 1);
cout << "r = " << r << endl;
return 0;
}
int main(void)
{
unsigned int x = 7;
unsigned int y;
y = x^(x >> 1);
y = y^(y >> 2);
y = y^(y >> 4);
y = y^(y >> 8);
y = y^(y >> 16);
cout << "y = " << y << endl;
unsigned int r = y & 1;
cout << "r = " << r << endl;
Basic Bitwise Operations 17
return 0;
}
int main(void)
{
unsigned int v1 = 8;
unsigned int r1 = p(v1);
cout << "r1 = " << r1 << endl;
unsigned int v2 = 9;
unsigned int r2 = p(v2);
cout << "r2 = " << r2 << endl;
unsigned int v3 = 101;
unsigned int r3 = p(v3);
cout << "r3 = " << r3 << endl;
return 0;
}
int main(void)
{
unsigned int x = 4;
unsigned int r = leading(x);
cout << "r = " << r << endl;
x = 100;
r = leading(x);
cout << "r = " << r << endl;
x = 255;
r = leading(x);
cout << "r = " << r << endl;
return 0;
}
Problem 65. The following C++ program utilizes the AND, OR and
shift operation. What is the output?
// reversing.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int x1 = 0;
unsigned int r1 = reversing(x1);
cout << "r1 = " << r1 << endl;
unsigned int x2 = 1;
unsigned int r2 = reversing(x2);
cout << "r2 = " << r2 << endl;
return 0;
}
Problem 66. The following C++ program uses the shift and OR opera-
Basic Bitwise Operations 19
#include <iostream>
using namespace std;
int main(void)
{
unsigned int x1 = 99;
unsigned int r1 = isqrt(x1);
cout << "r1 = " << r1 << endl;
unsigned int x2 = 100;
unsigned int r2 = isqrt(x2);
cout << "r2 = " << r2 << endl;
return 0;
}
Problem 67. The following program uses the shift operation. What is
the output?
// cuberoot.cpp
#include <iostream>
using namespace std;
b = (3*y*(y+1) + 1) << s;
s -= 3;
if(x >= b) { x -= b; y += 1; } // end if
} // end while
return y;
}
int main(void)
{
unsigned int x = 100;
unsigned int r1 = cr(x);
cout << "r1 = " << r1 << endl; // =>
x = 200;
unsigned int r2 = cr(x);
cout << "r2 = " << r2 << endl; // =>
return 0;
}
int main(void)
{
unsigned int j = 66;
unsigned int result = shiftOR(j);
cout << "result = " << result << endl;
return 0;
}
Problem 69. The function int f(int) in the following C++ program
uses the XOR operation and shift operation. What is the output?
// absolute.cpp
#include <iostream>
Basic Bitwise Operations 21
int f(int i)
{
int t = sizeof(int);
int r;
r = (i^(i >> 31))-(i >> 31);
return r;
}
int main(void)
{
int n1 = -87;
int r1 = f(n1);
cout << "r1 = " << r1 << endl;
int n2 = 99;
int r2 = f(n2);
cout << "r2 = " << r2 << endl;
return 0;
}
Problem 70. The following C++ program uses the AND operation and
shift operation. What is the output?
// countingbits.cpp
#include <iostream>
using namespace std;
int main(void)
22 Problems and Solutions
{
unsigned int v1 = 14;
unsigned int c1 = Table[v1 & 0xff] + Table[(v1 >> 8) & 0xff]
+ Table[(v1 >> 16) & 0xff] + Table[v1 >> 24];
cout << "c1 = " << c1 << endl;
unsigned int v2 = 234;
unsigned int c2 = Table[v2 & 0xff] + Table[(v2 >> 8) & 0xff]
+ Table[(v2 >> 16) & 0xff] + Table[v2 >> 24];
cout << "c2 = " << c2 << endl;
return 0;
}
int main(void)
{
unsigned int x = 17; unsigned int y = 23;
unsigned int result = add(x,y);
cout << "result = " << result << endl;
return 0;
}
Problem 72. The following C++ program uses the XOR, AND and shift
operation. What is the output?
// parity2.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned char v1 = 19;
unsigned int r1 = parity(v1);
cout << "r1 = " << r1 << endl;
unsigned char v2 = 18;
unsigned int r2 = parity(v2);
cout << "r2 = " << r2 << endl;
return 0;
}
Problem 73. The following C++ program uses the shift and AND op-
eration. What is the output?
// count.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i = 234;
unsigned int c;
for(c=0;i>0;i>>=1) { c += i & 1; }
cout << "c = " << c << endl;
return 0;
}
Problem 74. The following C++ operation applies the shift operation.
What is the output?
// signbit.cpp
#include <iostream>
using namespace std;
int main(void)
{
int i1 = 345271;
int r1 = (i1 >> 31);
cout << "r1 = " << r1 << endl;
int i2 = -471273;
int r2 = (i2 >> 31);
cout << "r2 = " << r2 << endl;
return 0;
}
Problem 75. The following C++ program uses the shift operation. What
is the output?
24 Problems and Solutions
// multiply.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i1 = 12;
unsigned int r1 = (i1 << 3) - i1;
cout << "r1 = " << r1 << endl;
unsigned int i2 = 12;
unsigned int r2 = (i2 << 4) + i2;
cout << "r2 = " << r2 << endl;
unsigned int i3 = 12;
unsigned int r3 = (i3 << 6) + i3;
cout << "r3 = " << r3 << endl;
unsigned int i4 = 12;
unsigned int r4 = (i4 << 3) + (i4 << 2) + i4;
cout << "r4 = " << r4 << endl;
return 0;
}
Problem 76. The following C++ program uses the shift operation and
AND-operation. What is the output?
// parity3.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int v1 = 10;
unsigned int r1 = parity(v1);
cout << "r1 = " << r1 << endl;
unsigned int v2 = 11;
unsigned int r2 = parity(v2);
cout << "r2 = " << r2 << endl;
return 0;
}
Basic Bitwise Operations 25
int main(void)
{
unsigned int x = 4;
unsigned int y = 4294967295;
unsigned int r = x^y;
int j = 31;
int count = 0;
while(j >= 0)
{
if((r & 1)==1) count++;
r = r >> 1;
j--;
}
cout << "count = " << count;
return 0;
}
Problem 78. The following C++ program uses the XOR, AND, OR and
shift operations. What is the output?
// bitsswapping.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i = 2, j = 5;
unsigned int n = 2;
unsigned int v = 24;
unsigned int result = swap(v,i,j,n);
cout << "result = " << result << endl;
return 0;
}
// overflow.cpp
#include <iostream>
using namespace std;
int main()
{
unsigned int i = 4294967295;
unsigned int j = 1;
unsigned int r = i+j;
cout << "r = " << r << endl;
return 0;
}
Use the binary representation of the number 4294967295 and the number
1.
// divide.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int i = 44;
unsigned int i0 = i & 0xFFFF;
unsigned int i1 = i >> 0x10;
unsigned int r0 = i0*0xAAAB;
unsigned int t = i1*0xAAAB + (r0 >> 0x10);
unsigned int r1 = t & 0xFFFF;
unsigned int r2 = t >> 0x10;
r1 += i0*0xAAAA;
unsigned p = i1*0xAAAA + r2 + (r1 >> 0x10);
unsigned result = p >> 1;
cout << "result = " << result;
return 0;
}
0 ~ one’s complement
1 & bitwise AND
2 ^ bitwise XOR
3 | bitwise OR
// precedence.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int a = 10;
unsigned int b = 11;
unsigned int c = 12;
unsigned int r1 = a & b | c;
cout << "r1 = " << r1 << endl;
unsigned int r2 = a & (b | c);
cout << "r2 = " << r2 << endl;
return 0;
}
28 Problems and Solutions
int main()
{
bitset<n> bs1;
bs1.flip(4);
cout << "bs1 = " << bs1 << endl;
bitset<n>* pbs1 = new bitset<n>;
pbs1 = &bs1;
cout << "pbs1 = " << pbs1 << endl;
bitset<n> bs2;
bs2.flip(7);
cout << "bs2 = " << bs2 << endl;
bitset<n>* pbs2 = new bitset<n>;
pbs2 = &bs2;
swap(pbs1,pbs2);
cout << "after swapping:" << endl;
cout << "bs1 = " << bs1 << endl;
cout << "bs2 = " << bs2 << endl;
return 0;
}
Problem 83. The following program uses the bitset class of C++. A
pointer to bitset is declared. What is the output of the following program?
// bitsetpointer.cpp
#include <iostream>
#include <bitset>
using namespace std;
int main(void)
{
const unsigned int n = 32;
bitset<n>* bp = new bitset<n>;
(*bp).set();
Basic Bitwise Operations 29
(*bp).flip(4);
cout << *bp << endl;
delete bp;
return 0;
}
Problem 84. The following program uses the bitset class of C++.
What is the output of the program?
// uinttobitset1.cpp
#include <iostream>
#include <bitset>
using namespace std;
int main(void)
{
const unsigned int n = 32;
bitset<n> s;
unsigned int i = 133;
convert(s,i,n);
cout << "s = " << s << endl;
return 0;
}
Problem 85. (i) Given an unsigned int number (32 bits). Write a C++
program that converts it to a bitstring of the bitset class.
(ii) Given an signed int number (32 bits). Write a C++ program that
converts it to a bitstring of the bitset class.
(iii) Given a floating point number float (32 bits). Write a C++ program
that converts it to a bitstring of the bitset class.
(iv) Given a floating point number double (64 bits). Write a C++ program
that converts it to a bitstring of the bitset class.
30 Problems and Solutions
Problem 86. (i) Given a bitstring of the bitset class of length 32. Write
a C++ program that converts the bitstring into an unsigned int. First one
has to check whether the bitstring refers to NaN.
(ii) Given a bitstring of the bitset class of length 32. Write a C++ program
that converts the bitstring into a signed int. First one has to check whether
the bitstring refers to NaN.
(iii) Given a bitstring of the bitset class of length 32. Write a C++
program that converts the bitstring into a float. First one has to check
whether the bitstring refers to NaN.
(iv) Given a bitstring of the bitset class of length 64. Write a C++
program that converts the bitstring into a double. First one has to check
whether the bitstring refers to NaN.
Advanced Bitwise
Manipulations
that returns x with the n bits that begin at position p set to the rightmost
n bits of y, leaving the other bits unchanged.
31
32 Problems and Solutions
which returns true if m and n are both even and false otherwise. Apply
bitwise operations.
that returns the value of the integer x rotated to the right by n bit positions.
Apply the shift operation.
Problem 11. Rewrite the following C++ program without the if ... else
using bitwise operations. What is the program doing?
Advanced Bitwise Manipulations 33
// iftoxor0.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(void)
{
unsigned int a, b, x;
srand((unsigned int) time(NULL));
x = rand()%2;
cout << "x = " << x << endl;
a = 0; b = 1;
if(x==a) x = b; else x = a;
cout << "x = " << x << endl;
return 0;
}
Write a C++ program using bitwise operations that implements this func-
tion.
Problem 13. Given an unsigned int (32 bits). Write a C++ program
with a function unsigned int lasttwo(unsigned int) that return the
integer number represented by the last two bits, i.e. if "00" return 0, if
"01" return 1, if "10" return 2 and if "11" return 3.
Problem 18. Write a C++ program that find the integer part log2 of an
unsigned int using bitwise operations.
#include <iostream>
using namespace std;
int main(void)
{
unsigned int x = 4000000000;
unsigned int y = 1000000001;
unsigned int r1 = (x + y)/2;
cout << "r1 = " << r1 << endl;
return 0;
}
Problem 20. The bitwise XOR operation ^ has a special property and
this can be used directly on encryption. Given two arbitrary bitstrings a
and b of the same length, then the following expression for bitwise XOR
holds
(a ^ b) ^ b = a .
In encryption the b would be called the key. Consider a character string, for
example ”Willi-Hans”. Write a C++ program that uses the XOR operation
that encrypts and decrypts this character string byte by byte. As a key use
Advanced Bitwise Manipulations 35
Problem 23. Give two different Gray codes for three bits.
// gray.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int g1 = 0;
unsigned int r1 = gray(g1);
cout << "r1 = " << r1 << endl;
unsigned int g2 = 1;
unsigned int r2 = gray(g2);
cout << "r2 = " << r2 << endl;
unsigned int g3 = 15;
unsigned int r3 = gray(g3);
cout << "r3 = " << r3 << endl;
unsigned int g4 = 256;
unsigned int r4 = gray(g4);
cout << "r4 = " << r4 << endl;
unsigned int g5 = 77;
unsigned int r5 = gray(g5);
cout << "r5 = " << r5 << endl;
return 0;
}
0 0 0 0 0 0
0 1 0 1 1 1
1 0 1 1 0 1
1 1 1 0 1 0
Write down the case for n = 4. Write C++ code that generates this
ordering.
Advanced Bitwise Manipulations 39
In the following use only the arithmetic operations for addition, subtraction,
multiplication, quotient and remainder.
1. Give the formula for N OT (a) without referring to the binary repre-
sentation.
2. Given an algorithm to calculate AN D(a, b) without referring to the
binary representation.
3. Give a formula for XOR(a, b) in terms of a, b and the AN D function.
4. Give a formula for OR(a, b) in terms of a, b and the AN D function.
5. Implement bitwise operations on integers using the answers above.
Problem 33. (i) Let xj ∈ {+1, −1} with j = 1, 2, . . . , 9. Find all solu-
tions of the system of six equations
x1 x2 x3 = 1, x4 x5 x6 = 1, x7 x8 x9 = 1,
40 Problems and Solutions
x1 x4 x7 = 1, x2 x5 x8 = 1, x3 x6 x9 = −1.
Write a C++ program that runs of all 29 combinations to find the solutions.
(ii) Let yj ∈ {0, 1}. Find all solutions of the system of six equations
y1 ⊕ y2 ⊕ y3 = 1, y4 ⊕ y5 ⊕ y6 = 1, y7 ⊕ y8 ⊕ y9 = 1,
y1 ⊕ y4 ⊕ y7 = 1, y2 ⊕ y5 ⊕ y8 = 1, y3 ⊕ y6 ⊕ y9 = −1
where ⊕ denotes the XOR-operation.
2.4 Theory
Problem 34. Let n ≥ 2 and even. How many bitstrings of length n can
one form with n/2 0’s and n/2 1’s. For n = 4 write down all of them in
lexicographical order.
b = b0 b1 b2 . . . bN −1 , c = c0 c1 c2 . . . cN −1
c 0 = b0
Advanced Bitwise Manipulations 41
c1 = b0 ⊕ b1
c2 = b0 ⊕ b1 ⊕ b2
..
.
cN −1 = b0 ⊕ b1 ⊕ · · · ⊕ bN −1
and
Discuss.
Problem 42. Consider the set {+1, −1} and multiplication. Then we
have the group table
· +1 -1
+1 +1 -1
-1 -1 +1
The neutral element of the group is +1. Consider now the set {0, 1} and
the XOR operation. Then we have the group table
⊕ 0 1
0 0 1
1 1 0
The neutral element of the group is 0. Show that the two group are iso-
morphic.
1, 0, 1, 0, 0, 0, 1, 0, 1, . . .
Write a C++ program that generates this sequence. Is the sequence chaotic?
p1 : 0 → 01, p2 : 1 → 10.
Find the first five elements in the construction of the Thue-Morse sequence.
Write a C++ program that generates the sequence. Use the bitset class.
f (x) = 2x mod 1.
We can associate with each point in [0, 1] an itinerary (an infinite sequence
of 0’s and 1’s) on which the shift map represents the action of f . Show that
all points in the subinterval
−n
[(k − 1)2−n , k 2 ], 1 ≤ k ≤ 2n
Show that this binary scalar product • is distributive over bitwise modulo
2 addition ⊕, i.e.
(x ⊕ y • z = (x • z) ⊕ (y • z).
Problem 48. Consider the full adder given by the truth table
A B C SUM CARRY
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Implement the full adder using only NAND-gates.
xj = integer part(rj ), j = 0, 1, 2, . . .
rj+1 = (rj − xj )nj
and
N
X xj
r0 ≈ x0 + .
j=1
n1 n2 · · · nj
Σ2 := { s := (s0 s1 s2 . . .) : sj = 0 or 1 }.
Problem 51. Periodic points are identified with exactly repeating se-
quences
s = (s0 s1 . . . sn−1 , s0 s1 . . . sn−1 , s0 s1 . . . sn−1 , . . .).
How many periodic points of period n there are?
Problem 53. (i) Given a digital circuit with 4 input lines and four
output lines. The output is the two’s complement of the input. Give the
truth table.
(ii) Find the disjunctive normal form.
(iii) Construct a logic circuit using a PAL.
(iv) Write a VHDL program tha simulates this circuit.
Problem 54. The genetic code vector space is presented by the Ga-
lois field of four bases (GF (4)). We consider the invertible map f :
{ A, C, G, T } → { (aj , aj+1 ) } from the base set to the set of binary du-
plets aj , aj+1 , where f (X) = (aj , aj+1 ) and aj ∈ {0, 1}. Since A maps to
T , C maps to G we introduce the map
f (A) = (0, 0), f (C) = (1, 0), f (G) = (0, 1), f (T ) = (1, 1).
{ G, A, T, C }, { G, T, A, C }, { C, A, T, G }, { C, T, A, G }
46 Problems and Solutions
{ A, C, G, T }, { A, G, C, T }, { T, C, G, A }, { T, G, C, A }
Write a C++ program using the bitset class that converts a given DNA
sequence, for example ”ATGCAATTCTCGCTA”, into the corresponding
bitstring.
Problem 55. Solve the following Max-SAT problem with five clauses and
four variables x1 , x2 , x3 , x4
X := X−m+1 , . . . , X−1 , X0 , X1 , X2 , . . .
Xk = Xk−` ⊕ Xk−m
i.e. f maps to an equal number of zeros and ones. Find all Boolean func-
tions from {0, 1}2 to {0, 1} which are either constant or balanced. Deter-
mine which of these functions are separable, i.e. for which function can we
find a hyperplane which separates the inputs x which give f (x) = 1 and
the inputs y which give f (y) = 0.
Advanced Bitwise Manipulations 47
0th → 000,
1st → 001,
..
.
7th → 111.
Problem 59. Show that the XOR-gate can be built from 4 NAND-gates.
Let A1 , A2 be the inputs and O the output.
f (x1 , x2 ) = x1 x2 .
We want to find the maxima of the (fitness) function f in the given domain
[−1, 1] × [−1, 1]. Consider the two bitstrings
b1 = "0000000011111111" b2 = "1010101010101010"
where the 8 bits on the right-hand side belong to x1 and the 8 bits on the
left-hand side belong to x2 . Find the value of the fitness function for the
two bitstring. Apply the NOT-operation to the two bitstrings to obtain the
new bitstrings b3 = N OT (b1) and b4 = N OT (b2). Select the two fittest of
the four bitstrings for survival.
Problem 61. Consider the domain [−1, 1]×[−1, 1] in R2 and the bitstring
(16 bits)
0101010100001111
The 8 bits on the right-hand side belong to x and the 8 bits on the left-hand
side belong to y (counting from right to left starting at 0). Find the real
48 Problems and Solutions
Find the value f (x∗ , y ∗ ). How far is this value from the minimum of f in
the given domain?
Problem 62. Show that the full-adder can be built with nine NAND-
gates. Give the circuit. Describe the circuit using multiexpression program-
ming.
0: a
1: b
2: NOT 0
3: OR 1,2
0: a
1: b
2: c
3: d
4: XOR 0,2
5: XOR 1,3
6: 0R 4,5
0: a
1: b
2: NOT 0
3: OR 1,2
0: a
1: b
2: c
3: d
4: XOR 0,2
5: XOR 1,3
6: OR 4,5
Problem 68. Build the 4 bit magnitude comparator using only NAND
gates.
Problem 69. Build the 8 bit priority encoder using only NAND gates.
Problem 71. In C++ the data type double consists of 64 bits. Convert
this bitstring of a double into a character array char a[8] counting from
left to right in the bitstring of the double.
50 Problems and Solutions
Problem 72. The data type float consists of 32 bits. Thus the number
of possible bitstrings is
232 = 68719476736
Apply the bitset class of C++ to generate all these bitstrings and convert
them into the corresponding float or NaN (not a number).
Problem 76. Let F2 be the field with the elements 0 and 1. Consider
the vector v := (v0 , v1 , . . . , vm−1 ) from the vector space Fm
2 . Then the
complement of the vector v is defined as
v̄ := (1 − v0 , 1 − v1 , . . . , 1 − vm−1 ).
n
We define a subset of vectors Wn ⊆ F22 as follows: We set W0 := {(0)} and
for j ≥ 0 the set Wj+1 consists of all vectors given by (w, w) and (w, w̄),
where w is any vector from Wj . Therefore Wj+1 has twice as many vectors
as the set Wj . One calls Wn the set of Hadamard vectors. The length of
the Hadamard vectors in Wn is 2n .
(i) Let W1 = {(0, 0), (0, 1)}. Find W2 .
(ii) Write a C++ program using the bitset class to generate the Hadamard
vectors.
Advanced Bitwise Manipulations 51
(0,1)
(-1,0) (0,0) (1,0)
(-1,0)
where σ0,0 , σ1,0 , σ−1,0 , σ0,1 , σ0,−1 ∈ {0, 1}, ⊕ is the XOR-operation and +
the arithmetic addition. Find the enery levels of Ĥ by running through all
possible bitstrings. There are 25 = 32 bitstrings of length 5. Write a C++
programs that finds the energy level using the bitset class of C++.
Problem 79. Let x ∈ {0, 1}. Consider the boolean functions (fj :
{0, 1} → {0, 1})
Problem 80. Consider the boolean function f : {0, 1}2 → {0, 1}2
f1 (x, y) = y ⊕ y, f2 (x, y) = x ⊕ x.
Problem 81. Consider the boolean function f : {0, 1}2 → {0, 1}2
f1 (x, y) = x ⊕ y, f2 (x, y) = x ⊕ y.
(iii) Start with (x, y) = (0, 1) and iterate the boolean function at least
twice. Discuss.
(iv) Start with (x, y) = (1, 1) and iterate the boolean function. Discuss.
Problem 82. Given the boolean function f : {0, 1}3 → {0, 1} as truth
table
x_1 x_2 x_3 f(x_1,x_2,x_3)
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1
Consider the unit cube in R3 with vertices (corner points) (0, 0, 0), (0, 0, 1),
(0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1). Find the plane in R3
that separates the 0 from the 1’s.
Problem 83. Consider the boolean function f : {0, 1}3 → {0, 1}3
where i_1, i_2 are the inputs and o is the output. Show that the XNOR-
gate can be build with five NAND-gates.
∂f
:= f (x1 , . . . , xj , . . . , xn ) ⊕ f (x1 , . . . , x̄j , . . . , xn )
∂xj x
for j = 1, . . . , n.
(i) Consider the boolen function f : {0, 1}2 → {0, 1} given by
f (x1 , x2 ) = x1 + x2 .
f (x1 , x2 ) = x1 · x2 .
f (x1 , x2 , x3 , x4 ) = x1 + x2 ⊕ x3 · x4 .
0 7→ 11, 1 7→ 10
for the first four steps starting with 1. Give a C++ implementation.
(iii) The Rudin-Shapiro sequence is generated by the two-digit substitutions
Start with 00 and give the sequence after three substitutions. Give a C++
implementation.
Problem 87. Consider the NAND gate. Show that the NOT gate can
be build with one NAND gate. Show that the AND gate can be build with
54 Problems and Solutions
two NAND gates. Show that the OR gate can be build with three NAND
gates. Show that the NOR gate can be build with four NAND gates. Show
that the XOR gate can be build with four NAND gates. Show that the
XNOR gate can be build with five NAND gates.
Problem 88. Consider the unit cube with the 8 corner points jk` (j, k, ` ∈
{0, 1})
000, 001, 010, 011, 100, 101, 110, 111.
Let xjk` ∈ {0, 1}. Each corner point has three nearest neighbours. Find all
the energy levels for
Problem 89. (i) Let s1 (0), s2 (0), s3 (0) ∈ {+1, −1}. Study the time-
evolution (t = 01, 2, . . .) of the coupled system of equations
s1 (t + 1) = s2 (t)s3 (t)
s2 (t + 1) = s1 (t)s3 (t)
s3 (t + 1) = s1 (t)s2 (t)
for the eight possible initial conditions, i.e. (i) s1 (0) = s2 (0) = s3 (0) = 1,
(ii) s1 (0) = 1, s2 (0) = 1, s3 (0) = −1, (iii) s1 (0) = 1, s2 (0) = −1, s3 (0) = 1,
(iv) s1 (0) = −1, s2 (0) = 1, s3 (0) = 1, (v) s1 (0) = 1, s2 (0) = −1, s3 (0) =
−1, (vi) s1 (0) = −1, s2 (0) = 1, s3 (0) = −1, (vii) s1 (0) = −1, s2 (0) = −1,
s3 (0) = 1, (viii) s1 (0) = −1, s2 (0) = −1, s3 (0) = −1. Which of these initial
conditions are fixed points?
(ii) Let s1 (0), s2 (0), s3 (0) ∈ {+1, −1}. Study the time-evolution (t =
01, 2, . . .) of the coupled system of equations
s1 (t + 1) = s2 (t)s3 (t)
s2 (t + 1) = s1 (t)s2 (t)s3 (t)
s3 (t + 1) = s1 (t)s2 (t)
for the eight possible initial conditions, i.e. (i) s1 (0) = s2 (0) = s3 (0) = 1,
(ii) s1 (0) = 1, s2 (0) = 1, s3 (0) = −1, (iii) s1 (0) = 1, s2 (0) = −1, s3 (0) = 1,
(iv) s1 (0) = −1, s2 (0) = 1, s3 (0) = 1, (v) s1 (0) = 1, s2 (0) = −1, s3 (0) =
−1, (vi) s1 (0) = −1, s2 (0) = 1, s3 (0) = −1, (vii) s1 (0) = −1, s2 (0) = −1,
Advanced Bitwise Manipulations 55
s3 (0) = 1, (viii) s1 (0) = −1, s2 (0) = −1, s3 (0) = −1. Which of these initial
conditions are fixed points?
Problem 90. Let x1 (0), x2 (0), x3 (0) ∈ {0, 1} and let ⊕ be the XOR-
operation. Study the time-evolution (t = 01, 2, . . .) of the coupled system
of equations
x1 (t + 1) = x2 (t) ⊕ x3 (t)
x2 (t + 1) = x1 (t) ⊕ x3 (t)
x3 (t + 1) = x1 (t) ⊕ x2 (t)
for the eight possible initial conditions, i.e. (i) x1 (0) = x2 (0) = x3 (0) = 0,
(ii) x1 (0) = 0, x2 (0) = 0, x3 (0) = 1, (iii) x1 (0) = 0, x2 (0) = 1, x3 (0) = 0,
(iv) x1 (0) = 1, x2 (0) = 0, x3 (0) = 0, (v) x1 (0) = 0, x2 (0) = 1, x3 (0) = 1,
(vi) x1 (0) = 1, x2 (0) = 0, x3 (0) = 1, (vii) x1 (0) = 1, x2 (0) = 1, x3 (0) = 0,
(viii) x1 (0) = 1, x2 (0) = 1, x3 (0) = 1. Which of these initial conditions are
fixed points?
Problem 92. Show that the full-adder can be built with nine NAND-
gates. Give the circuit. Describe the circuit using multiexpression program-
ming.
Show that this binary scalar product • is distributive over bitwise modulo
2 addition ⊕, i.e.
(x ⊕ y • z = (x • z) ⊕ (y • z).
where · is the AND operation and ⊕ is the XOR operation. Write a C++
program that finds the function f for all 16 possible inputs utilizing the
bitset class of the standard template library. The function plays a role
for the CHSH game.
a · b · c = a + b + c.
a · b · c = a ⊕ b ⊕ c.
a + b + c = a ⊕ b ⊕ c.
Binary Matrices
If we have two binary matrices of the same size, then the Hadamard product
(also called Schur product) would be an entry-wise AND-operation. For
example
1 0 1 1 1 0
• = .
1 0 0 1 0 0
The n × n permutation matrices are binary matrices, all of whose columns
and rows have each exactly one nonzero element. An adjacent matrix in
58
Binary Matrices 59
graph theory is a matrix whose rows and columns represent the vertices
and whose entries represent the edges of the graph. The adjaceny matrix
of a simple undirected graph is a binary symmetric matrix with 0 diagonal.
Problem 6. Consider the bitstrings 00, 01, 10, 11 and the XOR-operation
⊕. We define
(a1 a0 ) ⊕ (b1 b0 ) = (a1 ⊕ b1 )(a0 b0 ).
Thus we have the (group) table
⊕ 00 01 10 11
00 00 01 10 11
01 01 00 11 10
10 10 11 00 01
11 11 10 01 00
Consider the 2 × 2 matrices
1 0 1 0 −1 0 −1 0
I2 = , A= , B= , C=
0 1 0 −1 0 1 0 −1
60 Problems and Solutions
and matrix multiplication. Find the group table. Show that the two groups
are isomorphic.
Problem 7. Consider the bistrings 00, 01, 10, 11 and the rotation oper-
ation with
where we include the 2×2 identity matrix. We can associate binary numbers
with a Pauli spin matrix (including the identity matrix I2 ) σj (j = 0, 1, 2, 3)
via a two-dimensional binary vector r(σj ) with
The first and second entries of this bit vector are written as r1 (σj ) and
r2 (σj ), respectively. Show that given the binary representation of a Pauli
matrix (including the identity matrix) we can recover the matrix via
r (σj ) r2 (σj )
σj = ir1 (σj )r2 (σj ) σ11 σ3 .
Problem 10. Given two binary matrices A and B. Show that the Kro-
necker product A ⊗ B is also a binary matrix.
Problem 11. Given two binary matrices A and B. Show that the direct
sum A ⊕ B is also a binary matrix.
is given by
det A = a11 a22 a33 + a12 a23 a31 + a13 a21 a32
−a13 a22 a31 − a11 a23 a32 − a12 a21 a33 .
det B = (b11 · b22 · b33 ) ⊕ (b12 · b23 · b31 ) ⊕ (b13 · b21 · b32 )
⊕(b13 · b22 · b31 ) ⊕ (b11 · b23 · b32 ) ⊕ (b12 · b21 · b33 ) .
Problem 14. The finite field GF (2) consists of the elements 0 and 1 (bits)
which satisfies the following addition (XOR-operation) and multiplication
(AND-operation) tables
⊕ 0 1 · 0 1
0 0 1 0 0 0
1 1 0 1 0 1
Here n is called the length of the code and k the dimension. An (n, k)
binary linear block code can be specified by any set of k linear independent
codewords b0 , b1 , . . . , bk−1 . One arranges the k codewords into a k × n
Binary Matrices 63
binary matrix G. This matrix G is called a generator matrix for all the
codewords C. Consider the generator matrix (n = 6, k = 3)
1 0 0 1 1 0
G = 0 1 0 1 0 1.
0 0 1 0 1 1
1 0 0 0 0 1 1
0 1 0 0 1 0 1
G= .
0 0 1 0 1 1 0
0 0 0 1 1 1 1
Find all the codewords.
1 0 0 0 0 1 1
0 1 0 0 1 0 1
G= .
0 0 1 0 1 1 0
0 0 0 1 1 1 1
Show that the sixteen codewords are
0000000
0001111
0010110
64 Problems and Solutions
0011001
0100101
0101010
0110011
0111100
1000011
1001100
1010101
1011010
1100110
1101001
1110000
1111111
Show the parity check matrix is
0 1 1 1 1 0 0
H = 1 0 1 1 0 1 0.
1 1 0 1 0 0 1
Problem 19. Study the Lie algebra s`(2, F), where charF = 2.
Chapter 4
Reversible logic gates are gates that function in both directions. CMOS
implementations of such gates are designed. A special pass transistor logic
family is applied: reversible MOS. Many different reversible logic gates are
candidates as a universal building blocks. The controlled NOT gate, the
Fredkin gate can be implemented. They dissipate very little energy. Owing
to their use of reversible truth tables, they are even candidates for zero-
power computing. Circuit synthesis take advantage of mathematical group
theory. Algorithms have been developed for the synthesis of arbitrary re-
versible circuits.
65
66 Problems and Solutions
f (a, a0 , b, b0 ) = (a · b0 ) ⊕ (a0 · b) .
Problem 3. Consider a two input gate (x, y) / two output gate (x0 , y 0 )
given by
x0 = a · x ⊕ b · y ⊕ c
y 0 = a0 · x ⊕ b0 · y ⊕ c0
where a, b, a0 , b0 , c, c0 ∈ { 0, 1 }.
(i) Let a = 0, b = 1, a0 = 1, b0 = 0 and c = c0 = 0. Find the output (x0 , y 0 )
for all possible inputs (x, y). Is the transformation invertible?
(ii) Let a = 1, b = 1, a0 = 1, b0 = 1 and c = c0 = 0. Find the output (x0 , y 0 )
for all possible inputs (x, y). Is the transformation invertible?
x01 = x1
x02 = x1 ⊕ x2
(x1 , x2 , x3 ) → (x1 , x3 , x2 )
68 Problems and Solutions
(a, b, c) → (a, a · b ⊕ c, a · c ⊕ b)
reversible?
Problem 12. Prove that the Fredkin gate is universal. A set of gates is
called universal if we can build any logic circuits using these gates assuming
bit setting gates are given.
S=A⊕B
C = A · B.
x01 = x1
x02 = x1 ⊕ x2
x03 = x1 ⊕ x2 ⊕ x3 .
x01 = x1
x02 = x1 ⊕ x2
x03 = x3 ⊕ (x1 · x2 ).
Reversible Logic Gates 69
x01 = x1
x02 = x2
x03 = x3
x04 = x4 ⊕ x1 ⊕ x2 ⊕ x3 .
(a, b, c) → (a, a · b + a · c, a · c + a · b)
Problem 18. Show that the map f : {0, 1}3 → {0, 1}3
abc xyz
000 -> 000
100 -> 100
010 -> 101
110 -> 011
001 -> 001
101 -> 010
011 -> 110
111 -> 111
is invertible. The map describes a reversible half-adder. If c = 0, then x is
the first digit of the sum a + b and y is the carry bit. If c = 1, then z is the
first digit of the sum a + b + c and y is the carry bit.
x01 = x1 ⊕ x3
x02 = x1 ⊕ x2
x03 = (x1 · x2 ) ⊕ (x1 · x3 ) ⊕ (x2 · x3 ).
x01 = x1 ⊕ x3
x02 = x1 ⊕ x2
x03 = (x1 + x2 ) ⊕ (x1 + x3 ) ⊕ (x2 + x3 ).
x01 = x1 ⊕ x3
x02 = x2 ⊕ x3 ⊕ (x1 · x2 ) ⊕ (x2 · x3 )
x03 = x1 ⊕ x2 ⊕ x3
x04 = x4 ⊕ x3 ⊕ (x1 · x2 ) ⊕ (x2 · x3 ) .
Problem 22. The NOT, AND and OR gate form a universal set of oper-
ations (gates) for boolean algebra. The NAND operation is also universal
for boolean algebra. However these sets of operations are not reversible sets
of operations. Consider the Toffoli and Fredkin gates
F REDKIN : {0, 1}3 → {0, 1}3 , F REDKIN (a, b, c) = (a, a·c+ā·b, a·b+ā·c)
where ā is the NOT operation, + is the OR operation, · is the AND oper-
ation and ⊕ is the XOR operations.
In C, C++, Java and Perl we have two types of floatimg point number,
namely f loat and double. For the data type float with 32 bits the value
is stored as we have
sign bit, 8 bit exponent, 23 bit mantissa
i.e.
byte 1 byte 2 byte 3 byte 4
SXXX XXXX XMMM MMMM MMMMM MMMM MMMM MMMM
For the data type double we have 64 bits in C++. The value of double is
stored as
sign bit, 11 bit exponent, 52 bit mantissa
This means
byte 1 byte 2 byte 3 byte 4 byte 8
SXXX XXXX XXXX MMMM MMMM MMMM MMMM MMMM ... MMMM MMMM
We can also do bitwise manipulations of floating point numbers, for exam-
ple on the data type
Problem 1. Write a C++ progran that changes a bit in the floating
point number double using , is the bitwise OR | and the shift operation
<<.
71
72 Problems and Solutions
// floating.cpp
#include <iostream>
using namespace std;
int main(void)
{
double x = 3.14159;
int* p = (int*) &x;
*(p+1) = *(p+1)^(1<<31); // short cut *(p+1) ^= (1<<31)
cout << "x = " << x << endl;
return 0;
}
Note that the data type double takes 64 bit and the sign bit is at bit
position 63. The data type int takes 32 bits, << is the shift left operation
and ^ is the XOR operation.
Problem 4. Write a C++ program using the bitset class which con-
verts the memory representation of an arbitrary data type to a bitset of
appropiate size and vice versa. In other words store the sizeof(T) bytes
for an instance of T in a bitset. Since float and int are both 32-bit (on 32
bit architectures) use the program to find an integer with the same binary
representation as a given value of type float.
// btod.cpp
#include <bitset>
#include <cassert>
#include <iostream>
Floating Point Numbers 73
return d;
}
int main(void)
{
74 Problems and Solutions
bitset<size<double>::bits>
b(string("0111111111111111111111111111111111111111111111110000000000000000"));
cout << map_bitset_to<double>(b) << endl;
return 0;
}
// double.cpp
#include <iostream>
#include <limits>
using namespace std;
struct double_bits {
unsigned long mantissa : 52; /* need to modify for 32-bit */
unsigned int exponent : 11;
unsigned int sign : 1;
};
union udouble {
double d;
double_bits bits;
};
int main(void)
{
union udouble u;
cout.precision(numeric_limits<double>::digits);
u.d = 3.14;
cout << u.d << endl;
cout << u.bits.sign << "\t" << u.bits.exponent
<< "\t" << u.bits.mantissa << endl;
u.bits.sign = 1;
cout << u.d << endl;
cout << u.bits.sign << "\t" << u.bits.exponent
<< "\t" << u.bits.mantissa << endl;
u.bits.exponent++;
cout << u.d << endl;
cout << u.bits.sign << "\t" << u.bits.exponent
<< "\t" << u.bits.mantissa << endl;
u.bits.mantissa--;
Floating Point Numbers 75
Cellular Automata
Problem 3. Consider the initial value problem for the cellular automata
xj (t + 1) = xj−1 (t) ⊕ xj (t) ⊕ xj+1 (t), t = 0, 1, 2, . . .
where j = {−15, −14, . . . , 0, . . . , 16} and cyclic boundary condition. The
initial values at t = 0 are x0 (0) = 1 otherwise 0. Write a C++ program
76
Cellular Automata 77
that implements this cellular automata using the bitset class. Display the
results.
which is rule 50. Show that starting with a random initial condition the
system settles down in a state of period 2.
Chapter 7
String Manipulations
Problem 4. Use the string class of C++ and declare an array of strings
78
String Manipulations 79
Problem 6. Given two strings not necessarily of the same length. Write
a C++ program that implements the Levenshtein distance (edit distance).
Problem 7. Let A be a finite alphabet and let A∗ be the set of all finitely
long words that can be a written in this alphabet. One denotes by AN and
AZ be the sets of all semi-infinite and infinite sequences of letters from A.
Let f be a map from A → A∗ that associates with any letter in A a finite
word.
where · stands for concatenation and n, m are positive integers. One defines
n = 1, m = 1 Golden mean or Fibonacci chain
n = 1, m = 2 Silver mean
n = 1, m = 3 Bronze mean
n = 2, m = 1 Copper mean
n = 3, m = 1 Nickel mean
80 Problems and Solutions
Give a C++ implementation utilizing the string class. Count the numbers
of A’s and B’s at each step.
String Manipulations 81
82 Bibliography
Bibliography
Books
Steeb, W.-H.
Introduction to Assembly Language and C++
International School for Scientfic Computing, 2008
Steeb W.-H.
Matrix Calculus and Kronecker Product with Applications and C++ Pro-
grams
World Scientific Publishing, Singapore (1997)
Mendelson E.
Boolean Algebra and Switching Circuits
Schaum’s Outline Series (1970)
Warren H. S.
Hacker’s Delight
Addison-Wesly, Boston (2003)
Papers
Index
83