import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class HelloWorld{ public static void main(String []args){ List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List doubles = numbers.stream().mapToDouble(Integer::doubleValue).boxed().collect(Collectors.toList()); List doubleSquares = numbers.stream().mapToDouble(i->i.doubleValue() * 2).boxed().collect(Collectors.toList()); System.out.println(doubles); System.out.println(doubleSquares); } }

# Java

# Introduction to Logical Shift And Its Use Cases

If like me, you have a degree in computer science, you must have encountered logical shifts during the course of your academic studies, if not, if you have learnt pretty much any programming language, you would have encountered them. The issue is that most people treat them as something to be aware of but not really understood. If you give me a couple of minutes, I hope to help simplify it to you.

**What is logical shift**

Logical shift is a type of **bitwise operation **that shifts/moves the bits in its operands in a particular direction. In simple terms, if you **logically** shift X by Y, it means that the operation will move the bits in X by Y positions. Still sounds geeky, let us break it down together.

There are two types of logical shifts, namely

- Right Shift (denoted by >> )
- Left Shift (denoted by << )

If you consider the definition above, **Right shift **means that the bits in (X) would be shifted to the right by Y places, also **Left shift **means that the bits in (X) would be shifted to the left by Y places.

**Example of Left Shift**

let X = 4 and Y = 2, lets find X << Y

Convert X to binary, then you have

X = 100 (i.e. the binary representation of 4 is 0000 0100 )

**Note: you should use the 8 digits representation**

If you find the binary calculation confusing, please read up about it here.

Now, to calculate (**X << Y)**, i.e we move the bits in X to the left by 2 places.

- before move X = 0000 0100
- after left move (1) , X = 0000 1000
- after leftmove (2) , X = 0001 0000

Now we convert (**0001 0000**) back to decimal and we have 2.

Hence, 4 << 2 == **16**.

**Example of Right Shift**

let X = 8 and Y = 2, lets find X >> Y

Convert X to binary, then you have

X = 1000 (i.e. the binary representation of 8 is** 0000 1000**).

Now, to calculate (**X >> Y)**, i.e we move the bits in X to the right by 2 places.

- before move X = 0000 1000
- after right move (1) , X = 0000 0100
- after right move (2) , X = 0000 0010

**Note: since the first 4 digits in the binary value of X is (0) and we are shifting to the right, we could have discared it and just used (1000).**

Now we convert (**0010**) back to decimal and we have 2.

Hence, 8 >> 2 is equal to **2**.

**Use cases of logical shifts (both left and right).**

**Left shift (<<) **is used to multiply a value by any power of 2 and **Right bit shift** (>>) is used to divide by any power of two. consider the table below

**Cheatsheet or Ways of calulating the logical shifts faster**

The left logical shift (<<) is the same as

Left Bit Shift | |||||
---|---|---|---|---|---|

# | Operation | X | Y | Resolution | Result |

1 | X << Y | 8 | 2 | 8 * pow(2,2) | 32 |

2 | X << Y | 2 | 4 | 2 * pow(2,4) | 32 |

3 | X << Y | 3 | 2 | 3 * pow(2,2) | 12 |

and,

Right Bit Shift | |||||
---|---|---|---|---|---|

# | Operation | X | Y | Resolution | Result |

1 | X >> Y | 64 | 4 | 64 / pow(2,4) | 4 |

2 | X >> Y | 4 | 2 | 4 / pow(2,2) | 1 |

3 | X >> Y | 8 | 2 | 8 / pow(2,2) | 2 |

So, that is all. Hope i have made the use of logical shift alot clearer to you.

# Java – Get the path of a file from the resources folder

Sometimes, you may wish to get the path of a file or resource stored within you resources folder. The java snippet below will help you return the path to a resource placed in the **resources** folder.

Consider the code structure displayed above, the file we wish to get the path to is called “a.txt”.

import java.net.URL; public class Main { public static void main(String[] args) { Main main = new Main(); System.out.println("Path = [" + main.getPath("file/a.txt") + "]"); } private String getPath(String fileName) { ClassLoader classLoader = getClass().getClassLoader(); URL resource = classLoader.getResource(fileName); return resource.getPath(); } }

The output is shown below

Path = [/D:/Personal/JavaCodes/Blog/out/production/Blog/file/a.txt]