0

i read several time about Bitwise Right Shift Operator (>>\<<) but i continue without understand them:(

can anyone explain to me?

i don't undertand what will be the next results:(

Half = 25 >> 1;

Half = 25 << 1;

This Question has been **Answered**

1

If I have a number like `13`

, its binary representation is `1101`

(i.e., `13 = 8 + 4 + 1 = 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0`

). If I do a bitshift of the number to the left by one bit, i.e., `13 << 1`

, it simply means that I shift all the bits by one position to the left (filling the new bits with zero), giving me `11010`

which is `16 + 8 + 2 = 26`

. And as you can see, this has the effect of multiplying the number by 2. If I do a bitshift of the number to the right by one bit, i.e., `13 >> 1`

, it simply means that I shift all the bits by one position to the right (discarding any bits that fall off), giving me `110`

which is `4 + 2 = 6`

. And again, the effect is the same as an integer division by 2 (which discards the remainder). Shifting by more bits just means a higher power of 2 (multiplication or division). In other words, this is just a quick and efficient way to multiply / divide a number by powers of 2. Or, it can be used to do other bit-wise manipulations (usually for low-level purposes).

0

(13<<1)

13=1101

then you move all to the left: 11010

but how you know that is '0' or '1'?

2

Each digit stands for a power of 2, just like decimal digits stand for a power of 10. For example, if you have a decimal number:

```
5498
5 4 9 8
5 * 10^3 + 4 * 10^2 + 9 * 10^1 + 8 * 10^0
5 * 1000 + 4 * 100 + 9 * 10 + 8 * 1
5000 + 400 + 90 + 8
5498
```

For binary numbers, it is the same thing (except you only have two digits (0,1) and the base is 2 instead of 10):

```
1101
1 1 0 1
1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0
1 * 8 + 1 * 4 + 0 * 2 + 1 * 1
8 + 4 + 0 + 1
13
```

As far as doing the conversion between binary and decimal numbers, there are a number of "mental" tricks to do it, or you can just do it on the calculator (Windows calculator or whatever). For example, with the number 13, that is easy to convert mentally, you just start with the highest power of 2 that fits within the number. For 13, the highest power of 2 that fits in it is 8. Then you subtract them, giving you `13 - 8 = 5`

. And you repeat the process, noting down a 1 for each power of two that can fit, and 0 for each that does not, until you reach the lowest power (`2^1`

). So, you get:

```
current decimal: 13
current binary: ????
highest power fitting in number is: 8
current decimal: 13 - 8 = 5
current binary: 1???
highest power fitting in number is: 4
current decimal: 5 - 4 = 1
current binary: 11??
the next power does not fit in number!
current decimal: 1
current binary: 110?
highest power fitting in number is: 1
current decimal: 1 - 1 = 0
current binary: 1101
Done!
```

The opposite conversion is just as easy, you just add the corresponding power of two for each 1 you encounter in the binary number. So, `1101 = 8 + 4 + 1 = 13`

.

This is very fundamental stuff, and when you've programmed a lot, you don't even need to do the conversions anymore between binary / hexadecimal / decimal.

0

the C\C++ have several ways\function for convert them.

the >> and << are dificulty to understand:(

sorry, if i insist... but seem very difulty to understand.

i see that we convert decimal to binary:

10d=1010b

understood. but how and what is moved?

(my problem isn't the convertion between decimal and binary, but what << and >> do:()

2

As their name implies, they *shift the bits* to the left or right. In other words, the bits are moved to the left or right by the given amount of positions. If I take the number 13, I get this:

For left-shift:

```
13 == 1101 == 13
13 << 1 == 11010 == 26
13 << 2 == 110100 == 52
13 << 3 == 1101000 == 104
```

And for right-shift:

```
13 == 1101 == 13
13 >> 1 == 110 == 6
13 >> 2 == 11 == 3
13 >> 3 == 1 == 1
13 >> 4 == 0 == 0
```

I don't think there is a way to put it more clearly than that.

2

It is easiest to understand what << and >> do when you consider a number as a binary value (which is where convertion between binary and decimal come into it). This is because these are bitwise operators (2 of several), that is when they operate they consider individual bits of the operand rather than the overall number.

Considering just << (shift left) the operation is to shift the bits of the left hand operand left by the value of the right hand operand.

Considering the operation `10 << 2`

the easiest way to understand what is happening is to consider the decimal value 10 in it's binary form 00001010b (I'm using 8 bits in this illustrantion but there would normally be more).

What happens is that all the digits in the binary representation are shifted (or moved) 2 places in the number to the left

00001010b --> 00001010xxb

The bottom 2 places are filled with 0

00001010xxb --> 0000101000b

And since this is an 8 bit number the top 2 digits are discarded

0000101000b --> 00101000b

And that is the answer converted back to decimal 00101000b = 40

Shift right is similar by the bits are shifted the other way `10 >> 2`

Shift each digit 2 places to the right in the number discarding digits that fall off the right hand end

00001010b --> xx000010b

Fill in 0's at the left

xx000010b --> 00000010b

That's you answer the result is 00000010b = 2

The majority of processors have an instruction that does this so shift left/right tends to be quite an efficient operation.

When looking at the over all results the shift left/right operations appear similar to multiply/divide by power of 2 i.e. `10 << 2`

gives the same result as `10 * pow(2, 2)`

. However the shift operations tend to be used when you are more interested in the values of the actual bits rather than the value of the entire integer and the actual shift processor instructions are quite often faster to execute than the multiplicatin ones.

0

As their name implies, they shift the bits to the left or right. In other words, the bits are moved to the left or right by the given amount of positions. If I take the number 13, I get this:

For left-shift:

13 == 1101 == 13

13 << 1 == 11010 == 26

13 << 2 == 110100 == 52

13 << 3 == 1101000 == 104

And for right-shift:

13 == 1101 == 13

13 >> 1 == 110 == 6

13 >> 2 == 11 == 3

13 >> 3 == 1 == 1

13 >> 4 == 0 == 0

I don't think there is a way to put it more clearly than that.

you said that???

For left-shift:

13 << X put the X zeros on right side(like i see from your sample)

for right-shift:

13 << X delete the digits from right to left. the X is the number of digits be deleted until '0'

(if i'm wrong, please tell me)

thanks to both;)

This question has already been answered. Start a new discussion instead.

Recommended Topics

Hi,

I'm creating a program were logs files in a certain directory are presented in a table format.

My issue here is, I created a filter option in which the ...

```
public class StudentListings{
private String name; // key field
private int ID;
private double GPA;
private int next;
private int size;
private StudentListing[] data;
public StudentListings(){
this.name= name;
this.id= ID;
this.gpa=GPA;
}
public StudentListings(){
next=0;
data= new StudentListings[Size];
size= Size;
} // end of constructor
...
```

i need code for how i set the connection i.e when i execute the project .exe it show me a form in which there is Database ...