# Shift left operator using pen and paper

Discussion in 'C++' started by Ian Collins, Aug 2, 2010.

1. ### Ian CollinsGuest

On 08/ 2/10 04:19 PM, BozonHE wrote:
> 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?

--
Ian Collins

Ian Collins, Aug 2, 2010

2. ### red floydGuest

On 8/1/2010 9:19 PM, BozonHE wrote:
> 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
> "

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

red floyd, Aug 2, 2010

3. ### BozonHEGuest

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
"

BozonHE, Aug 2, 2010
4. ### Alf P. Steinbach /UsenetGuest

* 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?

> 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

--
blog at <url: http://alfps.wordpress.com>

Alf P. Steinbach /Usenet, Aug 2, 2010
5. ### Stuart GolodetzGuest

BozonHE wrote:
> 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

Stuart Golodetz, Aug 2, 2010
6. ### BozonHEGuest

In article <i35gpn$kg$-september.org>,
lid says...
>
> On 8/1/2010 9:19 PM, BozonHE wrote:
> > 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

paper.

BozonHE, Aug 2, 2010
7. ### BozonHEGuest

In article <i364qk$cc0$>, says...
>
> BozonHE wrote:
> > 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...?
>

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

Thank you.

I just forgot how to do such operations using pen and paper.

BozonHE, Aug 2, 2010
8. ### BozonHEGuest

In article <>,
says...
>
> 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
bitch about "homework" (in summer?!) or other bullshit.

Thank you

BozonHE, Aug 2, 2010
9. ### Stuart GolodetzGuest

BozonHE wrote:
> In article <i364qk$cc0$>, says...
>> BozonHE wrote:
>>> 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...?
>>

> Can you show me example how to shift left int 7 by four using this
> technique? Because I have problems with that.
>
> Thank you.
>
> 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

Stuart Golodetz, Aug 2, 2010
10. ### Öö TiibGuest

On 2 aug, 20:53, BozonHE <> wrote:
> In article <>,
> says...
>
>
>
> > 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.

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.

Öö Tiib, Aug 2, 2010
11. ### Stuart GolodetzGuest

Öö Tiib wrote:
> On 2 aug, 20:53, BozonHE <> wrote:
>> In article <>,
>> says...
>>
>>
>>
>>> 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.

>
> 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

Stuart Golodetz, Aug 2, 2010
12. ### Kai-Uwe BuxGuest

Öö Tiib wrote:

> On 2 aug, 20:53, BozonHE <> wrote:
>> In article <>,
>> says...
>>
>>
>>
>> > 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.

>
> 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

Kai-Uwe Bux, Aug 2, 2010
13. ### Daniel PittsGuest

On 8/1/2010 9:19 PM, BozonHE wrote:
> 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.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Daniel Pitts, Aug 2, 2010
14. ### BozonHEGuest

In article <>,
says...
[...]

Thank you for great explanation!

BozonHE, Aug 2, 2010
15. ### BozonHEGuest

In article <i371or$o0a$>, says...
>

[...]

Thank you for another great explanation!

Great job.

BozonHE, Aug 2, 2010
16. ### BozonHEGuest

In article <>,
says...
>
> BozonHE <> writes:
>

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

>
> 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.

BozonHE, Aug 2, 2010
17. ### Thomas J. GritzanGuest

Am 02.08.2010 23:37, schrieb Kai-Uwe Bux:
> Öö Tiib wrote:
>
>> On 2 aug, 20:53, BozonHE <> wrote:
>>> In article <>,
>>> says...
>>>
>>>
>>>
>>>> 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.

>>
>> 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.

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.

--
Thomas

Thomas J. Gritzan, Aug 2, 2010
18. ### Francesco S. CartaGuest

BozonHE <>, on 02/08/2010 04:19:13, wrote:

> 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 ]

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com

Francesco S. Carta, Aug 3, 2010