The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by , 2015-11-22 17:38:02

hack

hack

C++ Hackers Guide Steve Oualline

Chapter 13: Clever but Useless

Not all hacks have a practical use. Some hackers invent very clever ways
of doing things just to show off some very clever code so they can have bragging
rights. Not that this is an entirely useless endeavor. Figuring how to say
exchange the value of two variables without using a temporary requires you to
know a lot about some of the more obscure aspects of the computer and of
programming. And experimenting and learning these skills is one of the true
attributes of a good hacker.

Hack 130: Flipping a Variable Between 1 and 2

The Problem: A variable flag can have the value 1 or 2. If it is 1 change
it to 2 and if it is 2 change it to 1.

Now the most straight forward way of doing this is:

if (flag == 1) {
flag = 2;

} else {
flag = 1;

}

The paranoid hacker would use a switch and check for out of range values:

// This is the best and safest solution if we don't
// care about speed
switch (flag) {

case 1:
flag = 2;
break;

case 2:
flag = 1;
break;

default:
assert(“flag is not 1 or 2” == 0);
abort(); // Make sure we stop

}

The previous bit of code is the preferred solution if we don't care about
speed and we wish to be both clear and paranoid.

But in this case our goal is to demonstrate how clever we are and produce
a very clever hack.

Page 201 Copyright 2008, Steve Oualline

C++ Hackers Guide Steve Oualline

The Hack: The variable can be toggled between the two values with one
simple subtraction statement:

flag = 3 – flag;

You can now take a few seconds to verify the correctness of this statement.

This code functions however it is incomplete. If you must include clever
code in your program like this, please please include comments telling the
people that come after you what you've done.

// This clever piece of code flips the flag variable
// between the values of 1 and 2.
flag = 3 – flag;

This is almost the perfect hack. It is clever, it is small, it is extremely fast,
and it is clear (after the addition of the comment). Now if we could just find
some program we could use it in.

Hack 131: Swapping Two Numbers Without a Temporary

The Problem: How do you swap two variables without using a temporary.

The Hack: Use the following code:

void swap(int& a, int& b) {
a ^= b;
b ^= a;
a ^= b;

}

This is the classic CS201 answer. But as hackers we are never content to
leave things alone. Because I'm a hacker I wanted to see how things operate in
the real world.

And I discovered another way of swapping variables without using a
temporary:

static void inline swap(int& a, int& b) {
int tmp = a;
a = b;
b = a;

}

Now you might wonder how I can claim that this doesn't use a temporary
when I declare a temporary variable on the second line of the function. The trick
is that I declared this function inline and turned on optimization. This left the
compiler free to figure out the best way of swapping the two variables.

Page 202 Copyright 2008, Steve Oualline

C++ Hackers Guide Steve Oualline

Let's take a look at what the optimizer did for us. The first test code looks
like:

int main()
{

int a = 1;
int b = 2;

print(a,b);
swap(a,b);
print(a,b);
return(0);
}

Running this through the compiler and taking a look at the assembly code
generated we can see that the compiler rewrote our main function:

print(a,b);
print(b,a);

The swap call was eliminated completely. So not only did the compiler

eliminate the temporary, it also eliminated all the code as well. You can't get
more optimized that that.

If we change our test to force the actual swapping of the variables we get
the following code:

mov reg1, a
mov reg2, b
mov a, reg2
mov b, reg1

Now registers don't count as temporary variables (at least if I'm doing the
counting) so again we have a case where we are swapping the variables without
a temporary.

This question lets us illustrate several attributes of a true hacker. The first
is a willingness to think outside the box. Anyone can look up the standard
answer on the Internet. But only a good hacker will ask the question “Hey, what
will happen if ....” and then perform a bunch of experiments answering that
question. If the hacker is really good you find yourself with an answer that is
surprising and much better than the “standard” answer.

Page 203 Copyright 2008, Steve Oualline

C++ Hackers Guide Steve Oualline

Hack 132: Reversing the Words In a String Without a
Temporary

The Problem: How do you reverse the word in a string without using a
temporary string.

The Solution: Reverse the string, then go through the string reversing the
words.

For example start with the string:

Now is the time

Reverse the entire string:

emit eht si woN

Then reverse the individual words:

time eht si woN
time the is woN
time the is Now

Turns out that moving a word from one end of a string to another is a
difficult operation. Reversing a string is simple. So instead of moving words to
their proper place, we reverse them in, then reverse the word to make it come
out the right way round.

The code is below:

#include <iostream>
#include <ctype.h>

// Given two character pointers, reverse
// the string between them
static void rev_chars(

char* ptr1,
char* ptr2
)
{
char tmp;

while (ptr1 < ptr2) {
tmp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = tmp;
ptr1++;
ptr2--;

Page 204 Copyright 2008, Steve Oualline






















































Click to View FlipBook Version