Problems and Solutions For Bitwise Operations
Problems and Solutions For Bitwise Operations
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 purpose of this book is to supply a collection of problems in bitwise
operations and string manipulations.
The material was tested in our lectures given around the world.
Any useful suggestions and comments are welcome.
The International School for Scientific Computing (ISSC) provides certificate courses for this subject. Please contact the authors if you want to do
this course.
e-mail addresses of the authors:
steebwilli@gmail.com
Home pages of the authors:
http://issc.uj.ac.za
Contents
1 Basic Bitwise Operations
1.1 Introduction . . . . . . . .
1.2 Quickies . . . . . . . . . .
1.3 Explain the Output of the
1.3.1 Bitwise Operations
1.3.2 Shift Operations .
1.4 bitset class . . . . . . . .
. . . . . . . . .
. . . . . . . . .
C++ Program
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
4
13
13
17
30
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
33
33
37
40
41
3 Binary Matrices
58
64
70
6 Cellular Automata
75
7 String Manipulations
77
Bibliography
80
Index
81
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
AND
0
1
0
1
0
0
0
1
0
0
1
1
OR
0
1
0
1
0
1
1
1
0
0
1
1
XOR
0
1
0
1
0
1
1
0
NOT
0 1
1 0
1.2
Quickies
Problem 3.
(i) Is
(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 ?
Problem 7.
(x y) + (y z) + (z x) = (x y) (y z) (z x)
where denotes the AND-operation, + the OR-operation and the XORoperation?
Let a, b, c { 0, 1 }. Is
Problem 8.
(a b) c = a (b c) ?
Problem 9.
a b = x y.
Problem 13.
x y = x y,
x y = x y.
x2
0
0
0
0
1
1
1
1
x1
0
0
1
1
0
0
1
1
x0
0
1
0
1
0
1
0
1
z
0
0
1
0
0
1
0
1
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?
Problem 16.
The bitwise NOT-operation is defined by 0 -> 1 and
1 -> 0. Thus in a bitstring the NOT-operation replaces 0s by 1s and
1s by 0s. How can the bitwise NOT-operation be implemented using the
XOR-operation?
Problem 17.
be simplified?
Problem 18. The truth table of a typical encoder with inputs a, b, c, d
and outputs c0, c1 is
a
1
0
0
0
b
0
1
0
0
c
0
0
1
0
d
0
0
0
1
c0
0
0
1
1
c1
0
1
0
1
b
0
0
1
1
0
0
1
1
cin
0
1
0
1
0
1
0
1
y
0
1
1
0
1
0
0
1
cout
0
0
0
1
0
1
1
1
Problem 22. 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)
Problem 23.
(a b) (a b) a (b b).
N
X
j
,
j
2
j=1
j { 0, 1 }
Problem 28.
Problem 29. Show that every boolean function f : {0, 1}n {0, 1} can
be expanded as follows
f (x1 , x2 , . . . , xn ) = x1 f (1, x2 , . . . , xn ) + x1 f (0, x2 , . . . , xn ).
Problem 30. Apply the expansion theorem given at the previous exercise
repeatedly to each variable of
f (x1 , x2 , x3 ) = x1 x2 + x2 x3
to obtain its disjunctive normal form.
Problem 31.
and
mod 1
sk =
k = 0, 1, 2, . . .
1 if xk 0.5
0 if xk < 0.5
Find the truth table. Discuss. Write a C++ program that generates the
truth table.
Problem 37.
Problem 40.
11
Problem 43. 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.
Problem 44. Let i1 , i2 , i3 be the input bits and o1 , o2 , o3 be the output
bits. We say that the input bits and the output bits pass the GHZ test if
they satisfy the system of boolean equation
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 45. 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 XORoperation. Discuss.
Problem 46. Prove (i) using a truth table, (ii) using the properties of
boolean algebra (algebraic proof)
(a) 1 + x = 1
(b) x
y = x y
(c) (x y) x = x y
(d) that is associative.
Problem 47.
1.3
1.3.1
13
Problem 50. In the following C++ program that bitwise AND & is
applied. What is the output?
// bitand.cpp
#include <iostream>
using namespace std;
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;
}
Problem 51.
// XORANDOR.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int
unsigned int
unsigned int
unsigned int
i
j
k
l
=
=
=
=
10;
11;
12;
13;
Problem 52. In the following C++ program the bitwise XOR ^ is used.
What is the output?
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;
15
Problem 55.
// output.cpp
#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 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;
17
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 NOToperation. What is the output?
// XORNOT.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int j;
unsigned int r = j ^ (~j);
cout << "r = " << r << endl;
return 0;
}
Problem 61.
the output?
// turnonbit.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int v = 19;
unsigned int r = v | (v + 1);
cout << "r = " << r << endl;
return 0;
}
1.3.2
Shift Operations
Problem 62. The following C++ program uses the shift operation and
the XOR operation. What is the output?
Problem 63.
// pparity.cpp
#include <iostream>
using namespace std;
unsigned int p(unsigned int v)
{
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return (0x6996 >> v) & 1;
}
int main(void)
{
unsigned int v1
unsigned int r1
cout << "r1 = "
unsigned int v2
unsigned int r2
cout << "r2 = "
unsigned int v3
unsigned int r3
cout << "r3 = "
= 8;
= p(v1);
<< r1 << endl;
= 9;
= p(v2);
<< r2 << endl;
= 101;
= p(v3);
<< r3 << endl;
19
return 0;
}
Problem 64.
The following C++ program uses the shift-operation.
What is the output?
// leadingzeros.cpp
#include <iostream>
using namespace std;
unsigned int leading(unsigned int x)
{
if(x==0) return 32;
unsigned int n = 0;
if(x <= 0x0000FFFF) { n += 16; x = x << 16; }
if(x <= 0x00FFFFFF) { n += 8; x = x << 8; }
if(x <= 0x0FFFFFFF) { n += 4; x = x << 4; }
if(x <= 0x3FFFFFFF) { n += 2; x = x << 2; }
if(x <= 0x7FFFFFFF) { n += 1; }
return n;
}
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;
unsigned int reversing(unsigned int x)
{
& 0x55555555)
& 0x33333333)
& 0x0F0F0F0F)
& 0x00FF00FF)
& 0x0000FFFF)
x;
int main(void)
{
unsigned int x1
unsigned int r1
cout << "r1 = "
unsigned int x2
unsigned int r2
cout << "r2 = "
return 0;
}
<<
<<
<<
<<
<<
= 0;
= reversing(x1);
<< r1 << endl;
= 1;
= reversing(x2);
<< r2 << endl;
Problem 66. The following C++ program uses the shift and OR operation. What is the output of the following C++ program?
// isqrt.cpp
#include <iostream>
using namespace std;
unsigned int isqrt(unsigned int x)
{
unsigned int m, y, b;
m = 0x40000000;
y = 0;
while(m != 0)
{
b = y | m;
y = y >> 1;
if(x >= b) { x = x - b; y = y | m; }
m = m >> 2;
}
return y;
}
int main(void)
{
unsigned int x1 = 99;
unsigned int r1 = isqrt(x1);
cout << "r1 = " << r1 << endl;
21
Problem 67.
the output?
// cuberoot.cpp
#include <iostream>
using namespace std;
unsigned int cr(unsigned int x)
{
int s = 30;
unsigned int y, b;
y = 0;
while(s >= 0)
{
y = y << 1;
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;
}
Problem 68.
// shiftOR.cpp
#include <iostream>
using namespace std;
shiftOR(unsigned int v)
>>
>>
>>
>>
>>
(v
1);
2);
4);
8);
16);
>> 1);
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>
using namespace std;
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?
Problem 71.
// output1.cpp
#include <iostream>
using namespace std;
unsigned int add(unsigned int x,unsigned int y)
{
unsigned int low = (x & 0xffff) + (y & 0xfff);
unsigned int high = (x >> 16) + (y >> 16) + (low >> 16);
23
Problem 72. The following C++ program uses the XOR, AND and shift
operation. What is the output?
// parity2.cpp
#include <iostream>
using namespace std;
unsigned int parity(unsigned char v)
{
v ^= v >> 4;
v &= 0xf;
// short cut for v = v & 0xf
return ((0x6996 >> int(v)) & 1);
}
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 operation. What is the output?
// count.cpp
#include <iostream>
using namespace std;
int main(void)
{
25
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?
// 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;
Problem 76. The following C++ program uses the shift operation and
AND-operation. What is the output?
// parity3.cpp
#include <iostream>
using namespace std;
unsigned int parity(unsigned int v)
{
v ^= v >> 16;
v ^= v >> 8;
v ^= v >> 4;
v &= 0xf;
return ((0x6996 >> v) & 1);
}
int main(void)
{
unsigned int v1
unsigned int r1
cout << "r1 = "
unsigned int v2
unsigned int r2
cout << "r2 = "
return 0;
}
Problem 77.
= 10;
= parity(v1);
<< r1 << endl;
= 11;
= parity(v2);
<< r2 << endl;
// Hammingunsigned1.cpp
#include <iostream>
using namespace std;
int main(void)
{
unsigned int x = 4;
unsigned int y = 4294967295;
unsigned int r = x^y;
27
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;
unsigned int swap(unsigned int v,unsigned int i,unsigned int j,
unsigned int n)
{
unsigned int temp = ((v >> i)^(v >> j)) & ((1 << n) - 1);
return (v ^ ((temp << i) | (temp << j)));
}
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;
}
Problem 79.
// overflow.cpp
#include <iostream>
using namespace std;
int main()
{
unsigned int i = 4294967295;
Use the binary representation of the number 4294967295 and the number
1.
Problem 80.
// 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;
}
Problem 81.
as follows:
0
1
2
3
~
&
^
|
ones complement
bitwise AND
bitwise XOR
bitwise OR
b | c;
<< endl;
(b | c);
<< endl;
29
1.4
bitset class
Problem 82. The following program uses the bitset class of C++ and
utilizes pointers. What is the output of the program?
// bitset1.cpp
#include <iostream>
#include <bitset>
using namespace std;
const unsigned int n = 32;
void swap(bitset<n>* b1,bitset<n>* b2)
{
*b1 = (*b1)^(*b2);
*b2 = (*b2)^(*b1);
*b1 = (*b1)^(*b2);
}
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;
31
int main(void)
{
const unsigned int n = 32;
bitset<n>* bp = new bitset<n>;
(*bp).set();
(*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;
void convert(bitset<32>& s,unsigned int i,int n)
{
int count = 0;
while(count < n)
{
int t = 1 & i;
if(t != 0) s.set(count,1);
else s.set(count,0);
i = i >> 1;
count++;
}
} // end convert
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.
Show that
Chapter 2
Advanced Bitwise
Manipulations
2.1
0101
0110
1001
1010
1100
35
// 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;
}
of n is even
an :=
1
if
the
number
of
1s
in the binary representation
of n is odd
Write a C++ program that finds this sequence.
Problem 15. Let v, d be unsigned integers. To find the remainder of
integer division v/d one use v%d. Write a C++ program that uses bitwise
operations to do this operation. We assume that d is of the form 1, 2, 4, 8,
... i.e. of the form 2n .
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
37
2.2
Gray Code
Problem 22. An n-bit Gray code is a sequence of all the n-bit binary
numbers, ordered in such a way that each number differs from its predecessor and its successor by exactly 1 bit and the first and last differ by 1 bit
too. Give a 2-bit Grey sequence.
Problem 23.
(0, 0, 1),
(0, 1, 0),
(0, 1, 1),
(1, 0, 0),
(1, 0, 1),
(1, 1, 0),
(1, 1, 1).
x1 = (0, 0, 1),
x2 = (0, 1, 0),
x3 = (0, 1, 1)
x4 = (1, 0, 0),
x5 = (1, 0, 1),
x6 = (1, 1, 0),
x7 = (1, 1, 1).
The traveling saleswoman does not like her husband. Thus she wants to
find the longest route starting at (0, 0, 0) and returning to (0, 0, 0) after
visiting each city once. Is there more than one solution?
(ii) Consider the traveling salesman problem and the eight cities given in
(i). The traveling salesman likes his girlfriend. Thus he wants to find the
shortest route starting at (0, 0, 0) and returning to (0, 0, 0) after visiting
each city once. What is the connection with the Gray code? Is there more
than one solution?
(0, 0, 0, 1),
(0, 0, 1, 0),
(0, 0, 1, 1),
(0, 1, 0, 0),
(0, 1, 0, 1),
(0, 1, 1, 0),
(0, 1, 1, 1),
(1, 0, 0, 0),
(1, 0, 0, 1),
(1, 0, 1, 0),
(1, 0, 1, 1),
(1, 1, 0, 0),
(1, 1, 0, 1),
(1, 1, 1, 0),
(1, 1, 1, 1).
Problem 27.
Problem 28. The fundamental quantum-dot cellular automata logic device is a three-input majority logic gate. The truth table containing all
possible input combinations (a, b, c) in Gray code is given by (O is the
output)
a
0
0
0
0
1
1
1
1
b
0
0
1
1
1
1
0
0
c
0
1
1
0
0
1
1
0
O
0
0
1
0
1
1
1
0
(i) Find the boolean function (sum of products). Can the expression be
simplified?
(ii) Show that the majority gate can implement an AND-gate and an ORgate.
(iii) Can it implement a NOT-gate?
Problem 29.
// gray.cpp
#include <iostream>
using namespace std;
unsigned int gray(unsigned int g)
{
g ^= (g >> 16);
g ^= (g >> 8);
g ^= (g >> 4);
39
2.3
Problem 30. Usually we describe arithmetic operation in terms of binary operations, but in some cases it is interesting to consider the reverse
problem. Let a, b N0 and a, b < 2n . Thus we can write the binary
representation
a=
n1
X
aj 2j ,
a0 , a1 , . . . , an1 {0, 1}
bj 2j ,
j=0
b=
n1
X
j=0
bj
0
1
0
1
N OT 2(aj
1
1
0
0
AN D2(aj , bj )
0
0
0
1
OR2(aj , bj )
0
1
1
1
XOR2(aj , bj )
0
1
1
0
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 representation.
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.
2.4
41
Theory
Problem 31. Let n 2 and even. How many bitstrings of length n can
one form with n/2 0s and n/2 1s. For n = 4 write down all of them in
lexicographical order.
Problem 32. Consider a binary string S of length n 1 with symbols
1 and 0. We define (S) as the number of 1s of S minus the number
of 0s. For example, (1001001) = 1. We call a string S balanced if
every substring T of consecutive symbols of S has 2 (T ) 2. Thus
1001001 is not balanced, since it contains the substring 00100. The
string 01010101 is balanced. Let bn be the number of balanced strings of
length n. Obviously we have b1 = 2 with the strings 0 and 1 and b2 = 4
with the strings 00, 01, 10 and 11. Find a recursion relation for
bn .
Problem 33.
Let
= 1 + a1 x + a2 x2 +
be a formal power series with coefficients in the field of two elements (characteristic 2). We define
0
otherwise
For example, a11 = 0 since 11 = 10112 and a36 = 1 since 36 = 1001002 .
Show that 3 + x + 1 = 0.
Problem 34.
c = c0 c1 c2 . . . cN 1
(1)
f (x1 , x2 , . . . , xn ) = x
1 f (0, x2 , . . . , xn ) x1 f (1, x2 , . . . , xn ).
(2)
and
K(t)
0
0
1
1
0
0
1
1
Q(t)
0
1
0
1
0
1
0
1
43
Q(t + 1)
0
1
0
0
1
1
1
0
+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 isomorphic.
Problem 40. The Cantor sequence is constructed as follows. Given the
natural numbers
n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, . . .
We write them in ternary notation as
0, 1, 2, 10, 11, 12, 20, 21, 22, 100, . . .
Then the Cantor sequence bn (n = 0, 1, 2, . . .) is defined as: if n in ternary
has only 0s and 2s, then bn = 1, otherwise we set bn = 0. Write down the
sequence for the first 9 terms. Write a C++ program that generates this
sequence.
Problem 41. The Thue-Morse sequence is defined as follows. Let V =
{ 0, 1 }, s0 = 0 (intial value of the sequence) and
p1 : 0 01,
p2 : 1 10.
We can associate with each point in [0, 1] an itinerary (an infinite sequence
of 0s and 1s) on which the shift map represents the action of f . Show that
all points in the subinterval
n
[(k 1)2n , k 2
],
1 k 2n
B
0
0
1
1
0
0
1
1
C
0
1
0
1
0
1
0
1
SUM
0
1
1
0
1
0
0
1
CARRY
0
0
0
1
0
1
1
1
45
j = 0, 1, 2, . . .
N
X
j=1
xj
.
n1 n2 nj
1
.
n1 n2 nN
Apply this approximation to r0 = 2/3 and the golden mean number with
nj = 2 for all j and N = 4. Thus the xj form a bit sequence.
Problem 47.
Let
2 := { s := (s0 s1 s2 . . .) : sj = 0 or 1 }.
X
|sj tj |
2j
j=0
t = (101010 . . .).
Show that
ab=a+b
using (i) truth tables and (ii) properties of boolean algebra (with a+1 = 1).
Problem 50. (i) Given a digital circuit with 4 input lines and four
output lines. The output is the twos 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 51. The genetic code vector space is presented by the Galois 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 duplets 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 (T ) = (1, 1).
{ G, T, A, C },
{ C, A, T, G },
{ C, T, A, G }
{ 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 52. Solve the following Max-SAT problem with five clauses and
four variables x1 , x2 , x3 , x4
f1 (x1 , x2 .x3 , x4 ) = x1 x2
f2 (x1 , x2 , x3 , x4 ) = x1 x3 x4
f3 (x1 , x2 , x3 , x4 ) = x1 x2
f4 (x1 , x2 , x3 , x4 ) = x1 x3 x4
f5 (x1 , x2 , x3 , x4 ) = x2 x3 x4
using backtracking branch-and-bound, where is the NOT and is the
OR.
47
n1
X
pj log(pj )
j=0
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 58. 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
values x and y for this bitstring. Let f : [1, 1] [1, 1] R be given
by
f (x, y) = x4 + y 4 .
Find the value f (x , y ). How far is this value from the minimum of f in
the given domain?
Problem 59. Show that the full-adder can be built with nine NANDgates. Give the circuit. Describe the circuit using multiexpression programming.
Problem 60.
0:
1:
2:
3:
a
b
NOT 0
OR 1,2
49
a
b
c
d
XOR 0,2
XOR 1,3
0R 4,5
a
b
NOT 0
OR 1,2
a
b
c
d
XOR 0,2
XOR 1,3
OR 4,5
1
1
0
1
1
1
Problem 66.
Let
0,0 , 1,0 , 1,0 , 0,1 , 0,1 {0, 1}.
51
Problem 71. Show that the NAND-operation is not associative in general, i.e. N (N (a, b), c) 6= N (a, N (b, c)). Consider the case a = 0, b = 0,
c = 1.
Problem 72.
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.
Problem 74. Let n be an odd number and n 3. Write a C++ program
using the bitset class that parses trough all bitstrings of length n. If the
number of 0s is larger then the number of 1s the output should be 0 and
the number of 0s is smaller then the number of 1s the output should be
1. Then write down the truth table (majority gate).
Problem 75.
(-1,0)
(0,1)
(0,0)
(-1,0)
(1,0)
f2 (x) = x x,
f3 (x) = x + x
where is the XOR-operation, is the AND-operation and + is the ORoperation. Find the fixed points of f1 , f2 and f3 .
Problem 77.
f2 (x, y) = x x.
f2 (x, y) = x y.
53
f1 (x, y, z) = y y y,
f2 (x, y, z) = z z z,
f3 (x, y, z) = x x x.
i_2
0
1
0
1
o
1
1
1
1
where i_1, i_2 are the inputs and o is the output. The XNOR-gate is
given by
i_1
0
0
1
1
i_2
0
1
0
1
o
1
0
0
1
where i_1, i_2 are the inputs and o is the output. Show that the XNORgate can be build with five NAND-gates.
Problem 82. Let n 1. Consider the boolean function f : {0, 1}n
{0, 1}. Then the boolean derivatives are defined as
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 .
Find the boolean derivatives f /x1 , f /x2 .
(ii) Consider the boolen function f : {0, 1}2 {0, 1} given by
f (x1 , x2 ) = x1 x2 .
Find the boolean derivatives f /x1 , f /x2 .
1 7 10.
Start with 0 and give the sequence for the first four steps. Give a C++
implementation.
(ii) Study the binary sequence given by the substitutions
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
00 7 0001,
01 7 0010,
10 7 1101,
11 7 1110.
Start with 00 and give the sequence after three substitutions. Give a C++
implementation.
Problem 84. 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
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 85. 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
E = x000 x001 x010 x100 + x001 x000 x011 x101
+x010 x000 x011 x110 + x011 x001 x010 x111
+x100 x000 x110 x101 + x101 x001 x100 x111
+x110 x111 x100 x010 + x111 x011 x110 x101
where is the XOR-operation and + is the arithmetic plus. Find the
energy level for all possible 28 = 256 configurations.
55
Problem 86. (i) Let s1 (0), s2 (0), s3 (0) {+1, 1}. Study the timeevolution (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,
s3 (0) = 1, (viii) s1 (0) = 1, s2 (0) = 1, s3 (0) = 1. Which of these initial
conditions are fixed points?
Problem 87. Let x1 (0), x2 (0), x3 (0) {0, 1} and let be the XORoperation. 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 88. The Cantor series approximation is defined as follows.
For arbitrary chosen integers n1 , n2 , . . . (equal or larger than 2), we can
j = 0, 1, 2, . . .
N
X
j=1
xj
.
n1 n2 nj
1
.
n1 n2 nN
Apply this approximation to r0 = 2/3 and the golden mean number with
nj = 2 for all j and N = 4.
Problem 89. Show that the full-adder can be built with nine NANDgates. Give the circuit. Describe the circuit using multiexpression programming.
Problem 90. A boolean function f : { 0, 1 }n { 0, 1 }m (m n) is
periodic with period p with respect to bitwise modulo 2 addition, i.e. for
all x we have
f (x) = f (x + p).
Give an example of such a periodic boolean function.
Problem 91. Let x = x1 x2 . . . xn , y = y1 y2 . . . yn , z = z1 z2 . . . zn be
three n-bit binary strings. Let denote bitwise addition modulo 2, i.e.
x y = z for all k, zk = xk + yk mod 2
We define a scalar product of x and y by
x y := (x1 y1 ) + (x2 y2 ) + + (xn yn ) mod 2.
Show that this binary scalar product is distributive over bitwise modulo
2 addition , i.e.
(x y z = (x z) (y z).
Problem 92.
Let x { 0, 1 }. Calculate
f1 (x, y, z) = (x(y+z)),
f2 (x, y, z) = (x(yz),
f3 (x, y, z) = (x+(yz)).
57
Chapter 3
Binary Matrices
=
0 1
1 0
1
0
1
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 1. (i) Find all 2 2 binary matrices.
(ii) Find the determinant of all these matrices with the underlying field R.
(iii) In modular arithmetic mod 2 we define for a binary 2 2 matrix
det 2(A) := (a11 a22 ) (a12 a21 )
where is the AND-operation and the XOR operation. Find det 2(A) for
all 2 2 binary matrices.
Problem 2.
matrices.
00
01
10
11
00
00
01
10
11
01
01
00
11
10
10
10
11
00
01
B=
11
11
10
01
00
1
0
0
1
,
C=
1
0
0
1
Consider the bistrings 00, 01, 10, 11 and the rotation oper-
R(00) = 00,
R(01) = 10,
R(10) = 01,
R(11) = 11.
0
1
,
11
1
0
0
1
R(001) = 100,
R(010) = 010,
R(011) = 101
R(100) = 010,
R(101) = 101,
R(110) = 011,
R(111) = 111
1
000 0
0
diagonal matrices
1
0 0
1 0 = I3 , 001 0
0
0 1
0
1
1
0
0
0
1 0
0
1 0 0
010 0 1 0 , 011 0 1 0 .
0 0 1
0 0 1
1 0 0
1 0 0
100 0 1 0 = I3 , 101 0 1 0
0 0 1
0 0 1
1 0 0
1 0
0
110 0 1 0 , 111 0 1 0 .
0
0 1
0
0 1
i
0
,
3 =
1
0
0
1
.
Binary Matrices
61
r(1 ) = (1 0),
r(2 ) = (1 1),
r(3 ) = (0 1).
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 )
3
.
j = ir1 (j )r2 (j ) 11
Problem 10. Given two binary matrices A and B. Show that the Kronecker 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.
Problem 12.
1
1
The determinant of
a11
A = a21
a31
a 3 3 matrix
a12 a13
a22 a23
a32 a33
1
0
.
1 0
0 1
0 0
0
1 1 1
0, 0 1 1.
1
0 0 1
1 1 0
1 0
1 1 0, 1 0
0 0 0
1 0
binary matrices
0
1 0 1
0, 0 1 0.
0
1 0 1
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
0
1
1
1
0
1 0
A = 0 1
1 0
0
1
binary matrices
1
1
B = 0
0,
0
1
0
0
0
1
0
1
1
1
0
1
1.
1
Problem 15. An (n, k) binary linear block code is a k-dimensional subspace of the n-dimensional vector space
Vn := { (b0 , b1 , . . . , bn1 ) : bj bj GF (2) }.
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 , . . . , bk1 . 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
Find all the codewords.
Problem 16.
0
1
0
0
0
0
1
0
0
0
0
1
0
1
1
1
1
0
1
1
1
1
.
0
1
1 0 0 1 1 0
G = 0 1 0 1 0 1.
0 0 1 0 1 1
Find the parity-check matrix H.
Problem 18.
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 zeropower computing. Circuit synthesis take advantage of mathematical group
theory. Algorithms have been developed for the synthesis of arbitrary reversible circuits.
Reversible circuits are applicable to nanotechnolgy, quantum and optical
computing as well as reducing power in CMOS implementation. In adiabatic circuits, current is restricted to flow across devices with low voltage
drop and the energy stored on their capacitors is recycled. One uses reversible energy recovery gate capable to realize functions { AN D, OR } or
{ N AN D, N OR }.
64
65
Problem 3.
given by
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?
Problem 4.
T (a, b, c) := (a, b, (a b) c)
where a
is the NOT operation, + is the OR operation, is the AND operation and is the XOR operation.
F (a, b, c) := (a, a b + a
c, a c + a
b)
where a
is the NOT operation, + is the OR operation, is the AND operation and is the XOR operation.
1. Express N OT (a) exclusively in terms of the FREDKIN gate.
2. Express AN D(a, b) exclusively in terms of the FREDKIN gate.
3. Express OR(a, b) exclusively in terms of the FREDKIN gate.
4. Show that the FREDKIN gate is invertible.
Thus the FREDKIN gate is universal and reversible (invertible).
Problem 7. The Toffoli gate T(x1 , x2 ; x3 ) has 3 inputs (x1 , x2 , x3 ) and
three outputs (y1 , y2 , y3 ) and is given by
(x1 , x2 , x3 ) (x1 , x2 , x3 (x1 x2 ))
where x1 , x2 , x3 { 0, 1 }, is the XOR-operation and the AND-operation.
Give the truth table.
Problem 8. A generalized Toffoli gate T(x1 , x2 , . . . , xn ; xn+1 ) is a gate
that maps a boolean pattern (x1 , x2 , . . . , xn , xn+1 ) to
(x1 , x2 , . . . , xn , xn+1 (x1 x2 . . . xn ))
where is the XOR-operation and the AND-operation. Show that the
generalized Toffoli gate includes the NOT-gate, CNOT-gate and the original
Toffoli gate.
Problem 9. The Fredkin gate F(x1 ; x2 , x3 ) has 3 inputs (x1 , x2 , x3 ) and
three outputs (y1 , y2 , y3 ). It maps boolean patterns
(x1 , x2 , x3 ) (x1 , x3 , x2 )
67
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.
Problem 13.
->
->
->
->
->
->
->
->
xyz
000
100
101
011
001
010
110
111
69
T OF F OLI(a, b, c) = (a, b, (a b) c)
F REDKIN (a, b, c) = (a, ac+
ab, ab+
ac)
where a
is the NOT operation, + is the OR operation, is the AND operation and is the XOR operations.
1. Express NOT(a) exclusively in terms of the TOFFOLI gate.
2. Express NOT(a) exclusively in terms of the FREDKIN gate.
3. Express AND(a,b) exclusively in terms of the TOFFOLI gate.
4. Express AND(a,b) exclusively in terms of the FREDKIN gate.
5. Express OR(a,b) exclusively in terms of the TOFFOLI gate.
6. Express OR(a,b) exclusively in terms of the FREDKIN gate.
7. Show that the TOFFOLI gate is reversible.
8. Show that the FREDKIN gate is reversible.
Thus the TOFFPLI and FREDKIN gates are eachuniversal and reversible
(invertible).
Chapter 5
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
SXXX XXXX
byte 2
byte 3
byte 4
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 example 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
<<.
Problem 2.
71
// 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 3. C++ and C provide six operators for bit manipulations.
These may only be applied to integral operands, that is char, short, int
and long,whether signed or unsigned.
&
|
^
~
<<
>>
bitwise AND
bitwise OR
bitwise XOR
ones complement (unary)
left shift
right shift
// btod.cpp
#include <bitset>
#include <cassert>
#include <iostream>
73
bitset<size<double>::bits>
b(string("0111111111111111111111111111111111111111111111110000000000000000"));
cout << map_bitset_to<double>(b) << endl;
return 0;
}
Problem 6.
// 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--;
Chapter 6
Cellular Automata
Problem 1. We consider a one-dimensional ring of N sites labelled sequentialy by the index i staring from zero, i.e. i = 0, 1, . . . , N 1. We
impose periodic boundary conditions, i.e. N 0. Each site i can take the
values ai = 0 or ai = 1. Let ai evolve as a function ai (t) of discrete time
steps t = 0, 1, 2, . . . according to the map
ai (t + 1) = (ai1 (t) + ai+1 (t))
mod 2.
Since the map involves the sum ai1 + ai+1 mod 2, it is a nonlinear map.
Give a C++ implementation for this map.
Problem 2. Consider the initial value problem of the two-dimensional
cellular automata
xi,j (t+1) = (xi+1,j (t)+xi,j+1 (t))xi1,j (t)xi,j1 (t)xi,j (t),
t = 0, 1, 2, . . .
t = 0, 1, 2, . . .
111
0
100
1
110 101
0
0
011
0
010
1
001
1
000
0
which is rule 94. Show that starting with a random initial condition the
system settles down in a stationary state.
(ii) Consider the one-dimensional elementary cellular automata
111
0
110 101
1
1
100
1
011
1
010
0
001
1
000
0
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
b 7 f (b) = a.
b 7 f (b) = ba.
b 7 f (b) = aa.
b 7 f (b) = dc,
c 7 f (c) = ab,
d 7 f (d) = db.
String Manipulations
79
80
Bibliography
Bibliography
Books
Steeb, W.-H.
Introduction to Assembly Language and C++
International School for Scientfic Computing, 2008
Hardy Y. and Steeb W.-H.
Classical and Quantum Computing with C++ and Java Simulations
Birkhauser-Verlag, Boston (2002)
Steeb W.-H.
Matrix Calculus and Kronecker Product with Applications and C++ Programs
World Scientific Publishing, Singapore (1997)
Mendelson E.
Boolean Algebra and Switching Circuits
Schaums Outline Series (1970)
Warren H. S.
Hackers Delight
Addison-Wesly, Boston (2003)
Papers
Index
AND-gate, 2
Associative, 5
J-K flip-flop, 42
Levenshtein distance, 78
Cantor sequence, 43
Determinant, 65
Direct sum, 61
Disjunctive normal form, 8
Doz function, 35
Dual code, 63
Encoder, 6
Encryption, 36
Feynman gate, 65
Fibonacci sequence, 78
Fredkin gate, 66, 67
Full adder, 6, 7, 48, 56
Galois field, 46
Generalized Fredkin gate, 67
Generalized Toffoli gate, 66
Generator matrix, 63
Gray code, 37
Hadamard vectors, 51
Half-adder, 67
Hamilton cycle, 57
Hamming code, 63
Hamming distance, 8, 34, 78
Kronecker product, 61