Hong Xu (xuhdev)

Hong’s Personal Technology Blog

How to Insert 1 Bit Into an Integer

| Comments

Inserting a bit into an integer, means to insert a bit into a specific position of an integer, with the highest bit removed. For example, for a one byte integer 11001010, if I insert a 1 at digit position 4, which is marked here 1100|1010 as |, the most significant bit, which is the left most 1, would be eliminated and the rest of the 3 bits on the left side of | would be shifted to the left for 1 bit.

This is not hard to implement, but surprisingly I cannot find any existing code snippet for such a simple job.

Here is the code snippet I wrote for C, but it can be easily translated into any other language. Here, T is any integer type.

// position is the position of the new bit to be inserted. insert_true indicates
// whether the newly inserted bit is true or false

T x; // The integer we are going to insert into
T y = x;
x <<= 1;
if (insert_true)
    x |= (((T) 1) << position);
    x &= ~(((T) 1) << position);
x &= ((~((T) 0)) << position);
y &= ~((~((T) 0)) << position);
x |= y;

Don’t hesitate to comment if you think there is a better (more elegant or faster) way.

Install PHP Extensions on Shared Hosts

| Comments

In this post, I will show you a workaround to install additional PHP extensions which are not available on your Linux/BSD shared host. Before you proceed, you should ask whether your shared hosting company has an official way to install or whether they can include the extension you need on their system, as this method is really only a workaround: it probably works, but there is no guarantee of stability, especially your shared hosting company upgrades PHP version.

I’ll assume you have the basic knowledge of how to use a UNIX shell.

Matching an Arbitrary Number Range in Regular Expression Using Capturing Groups

| Comments

To match an arbitrary number range in a regular expression, is to match a string with a specific pattern which contains a number range. For example, to match with a pattern test{0..100}, where {0..100} denotes an integer not smaller than 0 and not larger 100, is such a case.

Although we already have some solutions to match a number range in regular expression, but they all make lengthy string and may lead to possible performance issue. In this post, I will provide an alternative solution to this problem, but it requires you to have control over the captured groups of the regex.

Back Up (Migrate) Homebrew Packages

| Comments

As one of the most popular package manager on OS X, Homebrew is indeed a very nice tool to manage packages. However, when you want to back up your packages, or migrate the packages onto another machine, Homebrew didn’t invent such a tool. If you are moving to an OS X of the same version, you can simply copy the Homebrew directory (default is /usr/local) to your new machine. Otherwise, we need to find a way to install the exactly same packages on your new machine. In order to accomplish the goal, I wrote a small piece of bash script to generate a restore script to install the packages.

Too Many Escaping Backslashes? Avoid Them!

| Comments

Backslash escaping is common in programming. Sometimes we may let a file go through a few filters or template engines, such as markdown, quik, etc. and things become even worse if we are writing the template files from a string which requires backslash escaping for any literal backslashes appearing in the string. On Windows, things are more horrible than on Unices (You know why, right? Hint: path separator). Then, if you need a “real” backslash in the final output, you may end up with four or eight or sixteen backslashes in the original file. This is horrible. To avoid this situation, I wrote a short preprocessing script in Python to double or quadruple or octuple or zzzuple your backslashes.

Use Both Homebrew and Macports on Your OS X

| Comments

Homebrew and Macports are two excellent package managers on OS X. At most of the time, Homebrew is fair enough: it has a large package collections. But sometimes, there are just some packages not available in Homebrew while they are in Macports. Although having both of them installed is not recommended, I still want to give it a try.

The basic rules here are using Homebrew packages as much as possible. When one package is not available in Homebrew, install it from Macports (you will soon see why). We will wrap the executables installed by Macports with suitable Environmental Variables.

Please note that this post only presents a workaround. It is not garanteed to work, and it is possible that it does not work under some certain circumstances.

Live Preview of LaTeX in Vim

| Comments

Vim, as a highly configurable and customizable editor, already has a powerful Vim-LaTeX plugin. However, I don’t like the lack of live preview feature. For this reason, I developed a plugin called vim-latex-live-preview last year. Although it currently can’t handle complex situations such as multiple tex files, it could basically handle single tex file project well (with or without BibLaTeX, thanks to Asis Hallab).

Note: This plugin currently doesn’t work on Windows.

Manage Multiple SSH Private Keys

| Comments

If you have multiple ssh keys, it is sometimes hard to manage. This was written before, but mostly they are handling using different keys with different host. What if you have two or more GitHub or BitBucket accounts requiring different keys? As a result, I created skm to provide a more general solution. Here I’ll give a brief introduction of how to manage multiple SSH private keys with skm.

Install and Use GNU Command Line Tools on Mac OS X

| Comments

If you are moving onto Mac OS X from Linux, you would probably find out that the command line tools shipped with Mac OS X are not as powerful and easy to use as the tools in Linux. The reason is that Mac OS X uses the BSD version command line tools, which are different from the Linux version, while they are both compliant with POSIX standards. But we can easily install the GNU tools by using Homebrew in Mac OS X and set them as default.

Note: you need to notice that you may have some compatibility issues with shell scripts written specifically for OS X after you have replaced your OS X commands with the GNU version. Although the very vast majority of shell scripts have no problem, you just need to be aware that when there comes a problem, this may be the spot to check on.