Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Meaning of <<

Posted on 2005-05-16
Medium Priority
233 Views
Hi

What's the meaning of "<<" at "\$i = \$h << \$c;" ?

Thanks
0
Question by:hasozduru
• 7
• 3

LVL 9

Expert Comment

ID: 14012382
it means bitwise shift left.

if \$h = 1 and \$c = 1 then \$i = 2
if \$h = 1 and \$c = 2 then \$i = 4
if \$h = 3 and \$c = 1 then \$i = 6
if \$h = 3 and \$c = 2 then \$i = 12
etc...
0

LVL 9

Expert Comment

ID: 14012394
it's all to do with binary data, i.e. the first example I gave above lloks like this:

00000001 << 00000001 = 00000010
0

LVL 9

Expert Comment

ID: 14012403
you see the bit moves up by the relevant amount of bits
0

Author Comment

ID: 14012423
Ok what does \$a <<= 2; mean?
0

LVL 9

Expert Comment

ID: 14012433
that means shift \$a to the left by two bits and assign the result to \$a
0

Author Comment

ID: 14012487
Let say \$a = 2 and we write \$a <<= 2; and then we type echo \$a. What will be the output?
0

LVL 9

Expert Comment

ID: 14012495
8
0

LVL 9

Accepted Solution

keteracel earned 2000 total points
ID: 14012514
00000010 << 2 = 00001000

and binary reads 128, 64, 32, 16, 8, 4, 2, 1
0

Author Comment

ID: 14012695
How does the following works? (if it's a odd value)

\$a = 3;
\$a <<= 2;
0

LVL 9

Expert Comment

ID: 14012755
if \$a is odd, then it seems that php does something different to other languages in that -1 shifted up by one is -2
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
###### Suggested Courses
Course of the Month14 days, 6 hours left to enroll