Shift left operator using pen and paper

I

Ian Collins

Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?

In decimal multiplication terms, what is the effect of shifting one
position to the left?
 
R

red floyd

Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

void main()
{
unsigned char X=7;
X<<= 4;
}

A) 28
B) 11
C) 112
D) 56
"

Your answer may be found in the FAQ. Specifically, FAQ 5.2.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
B

BozonHE

Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

void main()
{
unsigned char X=7;
X <<= 4;
}

A) 28
B) 11
C) 112
D) 56
"
 
A

Alf P. Steinbach /Usenet

* BozonHE, on 02.08.2010 06:19:
Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

For a guide to answering this, see Ian Collins' reply else-thread.

void main()

This 'void' means that your educational material is sub-standard.

You should so inform your teacher (you can cite the C++ standard §3.6.1/2 "It
shall have a return type of 'int'...").

Also the lack of anything named 'ByteVal' in the code, as referred to by the
question, indicates sub-standard teaching material.

{
unsigned char X=7;
X<<= 4;
}

A) 28
B) 11
C) 112
D) 56
"


Cheers & hth.,

- Alf
 
S

Stuart Golodetz

BozonHE said:
Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

void main()
{
unsigned char X=7;
X <<= 4;
}

A) 28
B) 11
C) 112
D) 56
"

This reminds me of the old joke: "There are 10 types of people - those
who understand binary and those who don't." :) That being said, here are
some base-neutral hints.

A number a_n ... a_1 a_0 in base b actually means:

\sum_{i=0}^n a_i b^i

Thus the decimal number 234 means:

2*(10^2) + 3*(10^1) + 4*(10^0)

Shifting a_n ... a_1 a_0 left by 1 yields:

a_n ... a_1 a_0 0

Or (if you prefer), a'_{n+1} a'_n ... a'_1 a'_0, where:

a'_{i+1} = a_i (for i >= 0)
a'_0 = 0

Thus, shifting 234 left by 1 in decimal yields:

2*(10^3) + 3*(10^2) + 4*(10^1) + 0*(10^0) = 2340

This is 10 times the original number. I wonder what would happen if you
did the same exercise in binary...?

Regards,
Stu
 
B

BozonHE

This reminds me of the old joke: "There are 10 types of people - those
who understand binary and those who don't." :) That being said, here are
some base-neutral hints.

A number a_n ... a_1 a_0 in base b actually means:

\sum_{i=0}^n a_i b^i

Thus the decimal number 234 means:

2*(10^2) + 3*(10^1) + 4*(10^0)

Shifting a_n ... a_1 a_0 left by 1 yields:

a_n ... a_1 a_0 0

Or (if you prefer), a'_{n+1} a'_n ... a'_1 a'_0, where:

a'_{i+1} = a_i (for i >= 0)
a'_0 = 0

Thus, shifting 234 left by 1 in decimal yields:

2*(10^3) + 3*(10^2) + 4*(10^1) + 0*(10^0) = 2340

This is 10 times the original number. I wonder what would happen if you
did the same exercise in binary...?


Can you show me example how to shift left int 7 by four using this
technique? Because I have problems with that.

Thank you.

ps. I'm NOT asking about answer for any "homwework" (in summer WTF?!).
I just forgot how to do such operations using pen and paper.
 
B

BozonHE

Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

void main()
{
unsigned char X=7;
X <<= 4;
}

A) 28
B) 11
C) 112
D) 56
"

btw. I know that answer is 112, but I dont really understand the way how
to do such operations using pen and paper. I just forgot how to do
binary shift by hand. And thats what im asking about. So please dont
bitch about "homework" (in summer?!) or other bullshit.

Thank you
 
S

Stuart Golodetz

BozonHE said:
Can you show me example how to shift left int 7 by four using this
technique? Because I have problems with that.

Thank you.

ps. I'm NOT asking about answer for any "homwework" (in summer WTF?!).
I just forgot how to do such operations using pen and paper.

7 (base 10) = 4+2+1 (base 10) = 111 (base 2)

Shift left 4:

1110000 (base 2) = 64 + 32 + 16 (base 10) = 112 (base 10)

And don't grouch -- it's undignified :) I explained everything you
needed to know (besides just giving you the answer on a plate) in my
original post. It's the "give a man a fishing rod" strategy.

Stu
 
Ö

Öö Tiib

btw. I know that answer is 112, but I dont really understand the way how
to do such operations using pen and paper.

Pencil and paper? You multiply seven with ten and get seventy. Then
you multiply seven with six and get forty two. Then you add seventy
and forty two and get hundred twelve.
 
S

Stuart Golodetz

Öö Tiib said:
Pencil and paper? You multiply seven with ten and get seventy. Then
you multiply seven with six and get forty two. Then you add seventy
and forty two and get hundred twelve.

Alternatively, you fold the paper to make a paper aeroplane, draw cool
markings on the side of it with the pencil, and then multiply 7 by 16 in
your head. YMMV :)
 
K

Kai-Uwe Bux

Öö Tiib said:
Pencil and paper? You multiply seven with ten and get seventy. Then
you multiply seven with six and get forty two. Then you add seventy
and forty two and get hundred twelve.

Way too complicated: 6 times 20 is 120; subtract 8 and you have 112.


Best

Kai-Uwe Bux
 
D

Daniel Pitts

Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

void main()
{
unsigned char X=7;
X<<= 4;
}

A) 28
B) 11
C) 112
D) 56
"

The easiest way is to memorize powers of two (and know how to calculate
the ones you don't have memorized), and then multiple (or divide, for
right shift) by corresponding power of two:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16

7 << 4 = 7*(2^4) = 7*(16) = 112

113 >> 4 = 113/(2^4) = 113/(16) = 7 remainder 1. Shifting drops
remainder, so just 7.

BTW, this works for unsigned integers only. If you are working with
signed data, the values and format will be different (twos complement vs
other representations may have different answers for the same problem).
One good way to handle those is to first convert to binary, and the
appropriate number of zeros, and convert back to decimal.
 
B

BozonHE

Yes, in summer. Not all homework is assigned by a professor, and not
all classes happen between fall and spring.


Don't take it so personally! We get homework questions here *very*
often, and yours does happen to look a lot like one. It's a reasonable
suspicion based on past group activity, not a personal attack on your
integrity - no need to get your hackles up about it.

sherm--

All right, now I see :) Thanks.
 
T

Thomas J. Gritzan

Am 02.08.2010 23:37, schrieb Kai-Uwe Bux:
Way too complicated: 6 times 20 is 120; subtract 8 and you have 112.

Huh? Everybody knows that a hexadecimal digit is 4 bits.
So left-shifting 0x7 by 4 bits makes it 0x70, which happens to be 112.
 
F

Francesco S. Carta

Hello. I have following question, can someone explain to me, how I can
shift left some value using only pen and paper? Or is there some clever
way without playing with binary numbers at all?


"After execution of the final assignment to ByteVal, what will be the
numeric value of X?

void main()
{
unsigned char X=7;
X<<= 4;
}

A) 28
B) 11
C) 112
D) 56
"

In addition to all the methods mentioned by the other responders, there
is another way which seems the most straightforward to me.

Double the number as many times as the shift value counts, in this case:

7 << 4 = 7 * 2 * 2 * 2 * 2 =
14 * 2 * 2 * 2 =
28 * 2 * 2 =
56 * 2 = 112

Replace "double" with "halve" for the right shift:

112 >> 4 = 112 / 2 / 2 / 2 / 2 =
56 / 2 / 2 / 2 =
28 / 2 / 2 =
14 / 2 = 7

If you get any decimal on the way simply discard it - for example:

113 >> 4

113, 56.5000, 28.2500, 14.1250, (7.0625 ==> 7)

or easier and faster:

113, (56.5 ==> 56), 28, 14, 7

[ digression

Here is some "Zen" method to calculate left shifts.

With one sheet of paper:
- ply it zig-zag 6 times (7-1) so that you get 7 "columns"
- open all the plies and flatten the sheet out again
- ply it in halves 4 times, following the perpendicular to the first
plies (that is, follow the "rows" direction, since the others were the
"columns").

Flattening out the sheet again and counting the "cells" will give the
result of the shift.

Plying a sheet of paper in halves more than three / four times, though,
can be a tough work, especially if the sheet is small and the paper is
heavy (thick).

-

With strings: take seven equally-long strings and tie them together with
a knot at half of their length, then group together the loose ends, add
another knot halfway through, regroup the endings and so on, four knots
in total.

Counting the "segments" (the strings going from knot to knot plus those
from the last knot to the loose ends) will give the result of the shift.

Also here, making a knot on a 56-strings "wire" can become a difficult
task, better to use long and thin strings.

-

Of course, the above methods can be improved by using multiplications
(rows * columns instead of counting cells, for example) but then it's
better to do this all by using just mathematics ;-)

By the way, I don't know any "Zen" way to calculate right shifts, can
anybody point out one?

end digression ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top