Saturday, 1 August 2020

Direct Conversion From Any Number Base To Another Base Explained

Let's consider that we need to convert a base 4 number to base 8 number.
Base 4 digits: 0 1 2 3
Base 8 digits: 0 1 2 3 4 5 6 7
We should build a chart to map all digits in base 8 to its equivalent number in base 4. That will come in handy.
So how do we do that? Looks like we need to do conversion from base 8 to base 4 first.
So here are the steps to follow:
  1. Divide the number to be converted (base 8) by the value of other base (4).
  2. Get the remainder. That is the least significant digit.
  3. Divide the quotient with the same value (4).
  4. Add the remainder as next least significant digit.
Keep doing steps 3 & 4 repeatedly till quotient becomes zero.  Now let's understand it with the example.
We need to convert 4, 5, 6, 7 to its equivalent base 4 number.

For 4:
      4 % 4 = 0, 4 / 4 = 1,
      1 % 4 = 1, 1 / 4 = 0
      So base 4 equivalent of 4 (base 8) is 10

For 5:
      5 % 4 = 1, 5 / 4 = 1
      1 % 4 = 1, 1 / 4 = 0
      So base 4 equivalent of 5 (base 8) is 11

For 6:
      6 % 4 = 2, 6 / 4 = 1
      1 % 4 = 1, 1 / 4 = 0
      So base 4 equivalent of 6 (base 8) is 12

For 7:
       7 % 4 = 3, 7 / 4 = 1
       1 % 4 = 1, 1 / 4 = 0
       So base 4 equivalent of 7 (base 8) is 13

Actually now you have learnt how to directly convert upper base numbers (base 8) to lower base numbers (base 4).
So what's the difference when we convert lower base (base 4) numbers to upper base (base 8)?
Just to remind, we were doing arithmetic operations in base 8 number system. As all digits of base 4 are already present in base 8 number system, we didn't have to do any extra step.
To convert from base 4 to base 8, we will be doing operations in base 4 number system. We need to divide the base 4 number by 8 which is the value of base we are converting to. But 8 is not present in base 4 system. And possible remainders can be 4, 5, 6, 7. So we need to find equivalent values for these numbers in base 4 system.
For 4, 5, 6, 7 we have already done that above.

Let's find equivalent of 8 (base 10) in base 4 system.
8 % 4 = 0, 8 / 4 = 2,
2 % 4 = 2, 2 / 4 = 0
So base 4 equivalent of number 8 (base 10) is 20.

Now we will convert 3211 in base 4 to its equivalent in base 8. Here is the hard part. We need to do the calculation in base 4 system.

Multiplication table:
1 * 20 = 20
2 * 20 = 20 + 20 = 100
3 * 20 = 100 + 20 = 120

  __130__________
20 | 3211
   20______
   121
   120______
     11
     
quotient: 130
remainder: 11 (base 8 equivalent is 5)
     
  __3______________   
20 | 130
   120_____
    10 

quotient: 3
remainder: 10 (base 8 equivalent is 4)

  __0________
20 | 3

quotient: 0
remainder: 3

So in the end we are left with 345. So 3211 in base 4 has been converted to 345 in base 8.

Do note that when we did base 8 to base 4 conversion above, I didn't create multiplication table. That was because I didn't want to confuse you in the beginning. It was handled implicitly as we were converting to higher base & that base was less than 10. But we were actually doing  arithmetic in base 8 system. So whenever you are doing direct conversion from one number base to another, make sure you have created the proper multiplication table first.

Difference Between Voluntary And Involuntary Context Switches

So I assume that you know what context switching is. Suppose you are running an OS in a dual core processor. And at some point, your application is running four threads. Operating system kernel needs to divide the CPU time among them. You are running four threads in two cores after all. This is the job of process scheduler which runs as a function in kernel. When invoked, it will look into a queue & select next thread which will get CPU time. This switching between threads can happen in two ways.
A thread can give up control of CPU as it will generally wait on I/O, user input. Or it might be sleeping. So another thread can be selected to start running on the same CPU core. This is called voluntary context switch.
But sometimes the thread is doing some CPU intensive task & runs out of CPU time scheduled for it. Then process scheduler will take the thread out & add it back to the queue. It will then put next thread on the CPU. This is called involuntary context switch.
Hope this clarifies the difference in simple terms. I might write some more on involuntary context switches later.

Solve Google Chrome Sound Issue In Windows

Just last week I solved this problem. In my Windows 10 laptop sound was fine. I was able to hear people in video chat. Even Microsoft Edge which is built-in browser for Windows was also working fine. But I was not getting any sound from Google Chrome. There can be various issues. But if there is no sound only in Chrome, then just check the below steps.


What happened in my case was that Chrome sound settings got disabled in Volume Mixer. So you need to right click on speaker icon on the task bar & then open Volume Mixer from the menu. There you should check if Google Chrome volume button is muted or not.
 

Friday, 31 July 2020

Do You Need To Submit Blogger Sitemap In Google Search Console

Google crawler should be able to index your blog posts eventually. You don't have to submit it. But it is recommended to submit your blogspot blog sitemap to Google Search Console. It might help you get your posts or pages indexed in Google quicker.
For this, you don't need any custom sitemap for Blogger blog. Sitemap is automatically generated by Blogger.

For posts:
http://blog-name.blogspot.com/sitemap.xml
http://custom-domain.com/sitemap.xml

For pages:
http://blog-name.blogspot.com/sitemap-pages.xml
http://custom-domain.com/sitemap-pages.xml

If you are using custom domain name, you need to use custom domain urls mentioned above. You can check urls for your blog in the browser & submit them in Google Search Console. If you don't have pages in your blog, then don't submit sitemap for pages.

Thursday, 30 July 2020

Use Notepad++ As Offline JSON Validator

You can find plenty of websites to validate & view JSON text. JSONLint is pretty popular. But I have had issues with them when file size goes beyond 3-4 mb. I mostly use this website to validate & parse JSON. It works well with big file size. So if you have a large JSON string to validate, it can be useful.
Now if you are on Windows & already using NotePad++, you can use it as an offline JSON validator tool. Just install this plugin. Now you can validate & see JSON text in tree view inside the text editor. It works well with large text also.

Wednesday, 29 July 2020

Difference Between Wait AND Park Methods In Java Thread

If you are familiar with thread dump or at least looked at it couple of times, probably you would have noticed "WAITING"  & "WAITING (parking)" states. So what's the difference?
In Java we have Objects.wait() and Unsafe.park() methods. Both of them will suspend the running thread & put it in waiting state. But these two methods work on different principles.
Object.wait() method works on monitor based synchronization. So that works well with "happens-before" relationship in Java. To bring back a waiting thread in runnable state, we will use Object.notify() method on the same monitor object. So when the thread comes back to runnable state, it will surely get updated values of the variables shared across multiple threads. JVM will make sure thread state is synchronized with main memory, but that is the additional overhead.
Unsafe.park() method takes the thread as an argument. To move back a parked thread to runnable state, we need to call Unsafe.unpark() method on the same thread. It works on permit basis. When Unsafe.unpark() is called, it will unblock the thread if it is already parked or will make sure the next park call on the thread is unblocked immediately. So its performance should be better as no synchronization is required with main memory. That's why thread pool in general (for example ExecutorService) use park method while waiting for tasks from the blocking queue.
As you can see, these use cases are different. If you have state that is shared across threads & you want to make sure one thread should wait for other thread before proceeding with the update, then you should go ahead with wait() and notify() methods. As an application developer, mostly you won't have to use park() method, it's too low level of an API.

Tuesday, 28 July 2020

Find Pattern And Its Anagrams In A Text

Previously I have written few posts related to pattern matching algorithms namely Rabin-Karp and KMP algorithms. So I thought of adding one layer of complexity on top of it. Now in addition to search a string, we need to find all its anagrams in the given text.
We can do it in O(n) time if we consider that character set in the given text would be of fixed length. For example, if we consider only English alphabet, we will have only 26 characters. You can check this link which explains it quite clearly. It uses Sliding Window technique. If you are familiar with it or have gone through Rabin-Karp Rolling Hash function already, then it is pretty easy to understand. Feel free to comment for any clarifications or if you have any other resource.

Direct Conversion From Any Number Base To Another Base Explained

Let's consider that we need to convert a base 4 number to base 8 number. Base 4 digits: 0 1 2 3 Base 8 digits: 0 1 2 3 4 5 6 7 We should...