Skip to content

Commit 07b2334

Browse files
author
ranch
committed
updated project 3 help
1 parent 08446ab commit 07b2334

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

docs/Teaching/C++/CS-2370/Projects/Project3.md

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## Friend keyword
44

5-
The friend keyword is used for when you're overloading a non-member function for
6-
your class. This is essential because we're literally defining the function of
7-
another class. For example, the equality operators or ostream output operator
8-
are all functions defined in other libraries. Since these functions do not have
9-
definitions for our object, we need to overload those function library methods
10-
so that they can handle our object.
5+
The friend keyword is used for when you're overloading a non-member function
6+
for your class, or providing general access to another class. This is essential
7+
because we're literally defining the function of another class. For example,
8+
the equality operators or ostream output operator are all functions defined in
9+
other libraries. Since these functions do not have definitions for our object,
10+
we need to overload those function library methods so that they can handle our
11+
object.
1112

1213
The big thing is that since it's relevant to our class, we define it in the body
1314
of our class. Make no mistake, it doesn't belong TO our class though. We're
@@ -18,7 +19,7 @@ private. If such data is private, the functions that we overload DON'T have
1819
access to our class data. Again, we're overloading ANOTHER class's functions to
1920
be able to handle our class. As such, it doesn't have access to our data.
2021

21-
Friend simply designates another class/class method as being able to access
22+
Friend simply designates another class/class-method as being able to access
2223
private data. Another class can't declare itself a friend and suddenly gain
2324
access to that private data. The class in question has to be willing to provide
2425
that private info.
@@ -43,7 +44,22 @@ Imagine if you wanted to use ints instead, but now you have to go through and
4344
replace every instance where ulong is. Let's say you want to switch it back.
4445
Well, we also use ints for other things like indices, and parameters, so it
4546
starts to become confusing as to which ints we actually want to change to ulong,
46-
and which are supposed to stay ints.
47+
and which are supposed to stay ints.
48+
49+
```cpp
50+
Bits(int b);
51+
void at(int p);
52+
```
53+
54+
In the example above, if we wanted to change the int type to a unsigned long
55+
long, which parameters do we change? It can become confusing.
56+
57+
```cpp
58+
Bits(IType b);
59+
void at(int p);
60+
```
61+
62+
This is muhc more clear about intent.
4763

4864
We're still limited though, if we want to be able to support bit operations on
4965
ints AND ulongs, we have to define two separate header files to account for
@@ -62,9 +78,11 @@ bytes, then multiplies the byte count by the amount of bits in a byte, and this
6278
gets us the total number of bits in a given type like "unsigned long long."
6379
We're just using this enum idiom to set that constant for us.
6480

81+
So, NBITS = sizeof(IType) * 8 -> 4Bytes * 8 -> 32Bits for an int.
82+
6583
## Bits
6684

67-
This is the real part that matters. For most bit operations refer to chapter 3
85+
This is the real part that matters. For most bit operations refer to chapter 3.11
6886
in zybooks. However, there may be some hiccups, especially when figuring out
6987
the solution to rotate, which is the hardest function in this project.
7088

@@ -84,22 +102,27 @@ void set(int pos){
84102
85103
Here is an example doing some bitwise operations in C++. You can
86104
easily turn this into a one-liner but for the sake of clearly understanding
87-
what's going on, I'm going to write it out line by line. We suppose bits is set to
88-
something like 1101. We want to set the 1st bit with the least
89-
significant bit (right-most) being 0th. We then create a 1 out of the type we're
90-
representing bits with, and bit shift to get our mask. In this case, we want to set
91-
bit at index 1 to be 1, so we move that 1 over to position 1. We then 'or' the
92-
bits with the mask and that sets the bit we want.
105+
what's going on, I'm going to write it out line by line.
106+
107+
1) We suppose bits is set to something like 1101. We want to set the 1st bit
108+
with the least significant bit (right-most) being 0th.
109+
110+
2) We then create a 1 out of the type we're representing bits with (IType),
111+
112+
3) Then we bit shift to get our mask. In this case, we want to set bit at index
113+
1 to be 1, so we move that 1 over to position 1.
114+
115+
4) We then 'or' the bits with the mask and that sets the bit we want.
93116
94117
## Rotate
95118
96-
I won't give you code for this one (it's just a few lines and not too bad)
119+
I won't give you code for this one (it's just a few lines)
97120
98-
However, the logic can be tricky to work out. Consider a right rotation. If we
99-
want to rotate bits to the right, some of them will fall off the right side,
100-
and will have to be moved in from the left. 00101 shifted right by one should
101-
become 10010. Every bit is shifted right by 1, and the 1 bit on the right-most
102-
side falls of, and rotates back to the left-most side.
121+
Consider a right rotation. If we want to rotate bits to the right, some of them
122+
will fall off the right side, and will have to be moved in from the left. 00101
123+
shifted right by one should become 10010. Every bit is shifted right by 1, and
124+
the 1 bit on the right-most side falls of, and rotates back to the left-most
125+
side.
103126
104127
So, in considering how to save bits and move them from one side to the other, we
105128
can think of the bits as two partitions.
@@ -170,3 +193,12 @@ So, for a negative rotation (e.g. you passed -3 to rotate) you can apply the sam
170193
logic as above, just perform this conversion operation on your position
171194
parameter, n, first e.g. n += NBITS; That will get you the correct number so you
172195
can still perform a right rotate.
196+
197+
### Alternate ways
198+
There are a number of ways that you can do these rotations, and quite frankly,
199+
there are several ways you can do any of these bitwise operations for the
200+
project. An alternative to the method I proposed above would be to simply
201+
create a bit mask of the bits that are going to fall off, preserve them,
202+
and then shift them appropriately so that they can be put into the correct
203+
position. The table at the bottom of zybooks 3.11 provides an example of this
204+
method.

0 commit comments

Comments
 (0)