## TASK #1 › Swap Odd/Even bits

### Task

You are given a positive integer `$N`

less than or equal to 255.

Write a script to swap the odd positioned bit with even positioned bit and print the decimal equivalent of the new binary representation.

## My solution

Like last weeks task, there are two ways of tackling this challenge. The first is to convert the number to the binary form, use some regular expression to swap the digits (for the record it would be `s/(.)(.)/$2$1/g`

), and then convert it back a decimal number. And there is nothing wrong with that approach.

My approach was to use the 'bitwise and' `&`

and shift bit operators `<<`

and `>>`

to calculate the value. We take the even values (170 = 2 + 8 + 32 + 128) and shift those one place to the right (the same as dividing by two), and the odd vales ( 85 = 1 + 4 + 16 + 64) and shift them to the left (same as multiplying by 2).

Given that the maximum value is 255, neither are really going to be more efficient than the other in the grand scheme of things.

### Examples

```
$ ./ch-1.pl 101
154
$ ./ch-1.pl 18
33
```

## TASK #2 › Clock Angle

### Task

You are given time `$T`

in the format `hh:mm`

.

Write a script to find the smaller angle formed by the hands of an analogue clock at a given time.

HINT: A analogue clock is divided up into 12 sectors. One sector represents 30 degree (360 ÷ 12 = 30).

### My solution

I follow these steps:

- Check the hour is between 1 and 12 and the minute value is between 0 and 59.
- If the hour value is 12, change it to zero.
- Calculate the angle (from the top) of the minute hand
`360 * $minute / 60`

. - Calculate the angle of the hour hand
`360 * ( $hour / 12 + $minute / 720 )`

- Find the absolute difference between these values
- If the difference is > 180°, calculate 360 minus the difference. This ensures we have the smallest angle.

### Examples

```
$ ./ch-2.pl 03:10
35°
$ ./ch-2.pl 04:00
120°
```

## Discussion (0)