Problem: given two numbers a and b find how many numbers with a bit x set are there in the interval [a,b].
Example:
Input: 5 8 2
Output: 3
Explanation:
5 = 0101
6 = 0110
7 = 0111
8 = 1000
The second bit (starting from the least significant bit with index 0) is set only in numbers 5, 6 and 7, so there are 3 numbers in the interval [5, 8] with the second bit set.
Solution:
A software engineer's technical blog about programming languages, computer graphics, problem solving, systems configuration, books and much more.
Saturday, June 11, 2016
Thursday, March 3, 2016
Remove all merged local branches in Git except master
$ git branch
a
b
c
* master
Here, branch 'c' has not merged with master yet. Let's remove all merged branches:
$ git branch --merged master | grep -v master | xargs git branch -d
Deleted branch a (was ebb9040).
Deleted branch b (was ebb9040).
$ git branch
c
* master
Sunday, January 31, 2016
A fast way to find out whether two elements in matrix share the same diagonal
1 2 3 4 5
-----------
1 | 0 0 0 0 0
2 | 0 0 1 0 0
3 | 0 0 0 0 0
4 | 1 0 0 0 0
5 | 0 0 0 0 0
with two points: (x1, y1) = (2, 3) and (x2, y2) = (4, 1).
The fastest way to find out whether these two points share the same diagonal is the result of this expression:
bool result = (x1 + y1 == x2 + y2) or (x1 - y1 == x2 - y2)
Now let's complicate the task. Given matrix n x n with m points, find the minimum number of diagonals which contain at least two points. The first line of the input contains: n m. The next m lines contain points with coordinates (xi, yi).
Saturday, January 16, 2016
dmalloc: compiling and installing a dynamic (shared) library
See the previous post: dmalloc: compiling and installing a static library
"dmalloc.h:484:7: error: expected identifier or..." problem is already solved in that post.
Now let's compile and install a shared library
dmalloc-5.5.2]$ ./configure --enable-shlib
dmalloc-5.5.2]$ make shlib
...
gcc -g -O2 -DHAVE_STDARG_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_W32API_WINBASE_H=0 -DHAVE_W32API_WINDEF_H=0 -DHAVE_SYS_CYGWIN_H=0 -DHAVE_SIGNAL_H=1 -I. -I. -c dmalloc_argv.c -o ./dmalloc_argv.o
rm -f dmalloc
gcc -o aout dmalloc.o dmalloc_argv.o compat.o env.o \
-L.
mv aout dmalloc
rm -f libdmalloc.so libdmalloc.so.t
ld -shared --whole-archive -soname libdmalloc.so -o libdmalloc.so.t libdmalloc.a arg_check.o compat.o dmalloc_rand.o dmalloc_tab.o env.o heap.o chunk.o error.o malloc.o
ld: libdmalloc.a(arg_check.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
libdmalloc.a(arg_check.o): error adding symbols: Bad value
Makefile:259: recipe for target 'libdmalloc.so' failed
make: *** [libdmalloc.so] Error 1
"dmalloc.h:484:7: error: expected identifier or..." problem is already solved in that post.
Now let's compile and install a shared library
dmalloc-5.5.2]$ ./configure --enable-shlib
dmalloc-5.5.2]$ make shlib
...
gcc -g -O2 -DHAVE_STDARG_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_W32API_WINBASE_H=0 -DHAVE_W32API_WINDEF_H=0 -DHAVE_SYS_CYGWIN_H=0 -DHAVE_SIGNAL_H=1 -I. -I. -c dmalloc_argv.c -o ./dmalloc_argv.o
rm -f dmalloc
gcc -o aout dmalloc.o dmalloc_argv.o compat.o env.o \
-L.
mv aout dmalloc
rm -f libdmalloc.so libdmalloc.so.t
ld -shared --whole-archive -soname libdmalloc.so -o libdmalloc.so.t libdmalloc.a arg_check.o compat.o dmalloc_rand.o dmalloc_tab.o env.o heap.o chunk.o error.o malloc.o
ld: libdmalloc.a(arg_check.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
libdmalloc.a(arg_check.o): error adding symbols: Bad value
Makefile:259: recipe for target 'libdmalloc.so' failed
make: *** [libdmalloc.so] Error 1
Tuesday, January 12, 2016
dmalloc: compiling and installing a static library
dmalloc - http://dmalloc.com/
Installation (on Fedora 21 x86_64)
$ uname -irs
Linux 3.18.3-201.fc21.x86_64 x86_64
$ tar -zxvf dmalloc-5.5.2.tgz
dmalloc-5.5.2]$ cd dmalloc-5.5.2/
dmalloc-5.5.2]$ ./configure
configure: configurations for the dmalloc library
configure: build utilities
checking for gcc... gcc
...
checking for strtok... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating conf.h
config.status: executing dmalloc.h.2 commands
config.status: executing settings.h commands
configure:
configure: Please check-out Makefile and conf.h to make sure that
configure: sane configuration values were a result.
configure:
configure: You may want to change values in settings.h before
configure: running 'make'.
configure:
configure: To run the basic library tests, you can execute:
configure: make light
configure: or
configure: make heavy
configure:
dmalloc-5.5.2]$ make
rm -f dmalloc.h dmalloc.h.t
cat ./dmalloc.h.1 dmalloc.h.2 ./dmalloc.h.3 > dmalloc.h.t
mv dmalloc.h.t dmalloc.h
rm -f arg_check.o
gcc -g -O2 -DHAVE_STDARG_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_W32API_WINBASE_H=0 -DHAVE_W32API_WINDEF_H=0 -DHAVE_SYS_CYGWIN_H=0 -DHAVE_SIGNAL_H=1 -I. -I. -c arg_check.c -o ./arg_check.o
In file included from /usr/include/string.h:634:0,
from arg_check.c:33:
dmalloc.h:484:7: error: expected identifier or ‘(’ before ‘__extension__’
char *strndup(const char *string, const DMALLOC_SIZE len);
^
Makefile:362: recipe for target 'arg_check.o' failed
make: *** [arg_check.o] Error 1
Installation (on Fedora 21 x86_64)
$ uname -irs
Linux 3.18.3-201.fc21.x86_64 x86_64
$ tar -zxvf dmalloc-5.5.2.tgz
dmalloc-5.5.2]$ cd dmalloc-5.5.2/
dmalloc-5.5.2]$ ./configure
configure: configurations for the dmalloc library
configure: build utilities
checking for gcc... gcc
...
checking for strtok... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating conf.h
config.status: executing dmalloc.h.2 commands
config.status: executing settings.h commands
configure:
configure: Please check-out Makefile and conf.h to make sure that
configure: sane configuration values were a result.
configure:
configure: You may want to change values in settings.h before
configure: running 'make'.
configure:
configure: To run the basic library tests, you can execute:
configure: make light
configure: or
configure: make heavy
configure:
dmalloc-5.5.2]$ make
rm -f dmalloc.h dmalloc.h.t
cat ./dmalloc.h.1 dmalloc.h.2 ./dmalloc.h.3 > dmalloc.h.t
mv dmalloc.h.t dmalloc.h
rm -f arg_check.o
gcc -g -O2 -DHAVE_STDARG_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_UNISTD_H=1 -DHAVE_SYS_MMAN_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_W32API_WINBASE_H=0 -DHAVE_W32API_WINDEF_H=0 -DHAVE_SYS_CYGWIN_H=0 -DHAVE_SIGNAL_H=1 -I. -I. -c arg_check.c -o ./arg_check.o
In file included from /usr/include/string.h:634:0,
from arg_check.c:33:
dmalloc.h:484:7: error: expected identifier or ‘(’ before ‘__extension__’
char *strndup(const char *string, const DMALLOC_SIZE len);
^
Makefile:362: recipe for target 'arg_check.o' failed
make: *** [arg_check.o] Error 1
Thursday, May 14, 2015
Convert images to pdf and back
Today I found out a simple and flexible tool which converts images to pdf and vice versa - convert. It was already built in my Fedora 21.
Example:
Let's say I have 1.jpg, 2.jpg, 3.jpg files. To convert them into a single result.pdf run the following command:
$ convert 1.jpg 2.jpg 3.jpg result.pdf
or
$ convert {1..3}.jpg result.pdf
Now I want to get my images back from the result.pdf:
$ convert -density 300 result.pdf image.jpg
where "-density 300" is a precision in points per inch. Each pdf page will be converted into a separate image.
convert supports a lot of different flags and options. To learn more about it:
$ man convert
or
Wednesday, April 1, 2015
log4j2: line separator in header's parameter of PatternLayout
Log4j2 provides a new parameter for PatternLayout:
Parameter name: header
Type: String
Description: the optional header string to include at the top of each log file
With the following log4j2.xml configuration:
Parameter name: header
Type: String
Description: the optional header string to include at the top of each log file
With the following log4j2.xml configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="20"> <Appenders> <RollingRandomAccessFile name="file" fileName="logs/app.log" filePattern="logs/app-%d_%i.log.gz"> <PatternLayout> <header> Version: ${env:APP_RELEASE_VERSION} </header> <pattern>%message%n</pattern> </PatternLayout> <Policies> <OnStartupTriggeringPolicy/> </Policies> </RollingRandomAccessFile> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="file"/> </Root> </Loggers> </Configuration> |
Git: undo the last rebase
$ git reflog
f0d821f HEAD@{0}: commit_a
2ea24f8 HEAD@{1}: commit_b
0c3cd49 HEAD@{2}: commit_c
de5b02a HEAD@{3}: commit_d
Suppose that "commit_c" was the head commit of the branch just before the rebase. Let's reset the current branch to this commit:
$ git reset --hard HEAD@{2}
f0d821f HEAD@{0}: commit_a
2ea24f8 HEAD@{1}: commit_b
0c3cd49 HEAD@{2}: commit_c
de5b02a HEAD@{3}: commit_d
Suppose that "commit_c" was the head commit of the branch just before the rebase. Let's reset the current branch to this commit:
$ git reset --hard HEAD@{2}
Playing with vim configuration for C++
What I got as a result:
The main features:
The main features:
- embedded file system explorer
- syntax highlighting
- hotkeys for compiling/running C++ code
Wednesday, February 11, 2015
Netty server is incompatible with ObjectOutputStream on the client side
Recently I faced with the following problem while playing with Netty:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | io.netty.handler.codec.TooLongFrameException: Adjusted frame length exceeds 1048576: 2901213193 - discarded at io.netty.handler.codec.LengthFieldBasedFrameDecoder.fail(LengthFieldBasedFrameDecoder.java:493) at io.netty.handler.codec.LengthFieldBasedFrameDecoder.failIfNecessary(LengthFieldBasedFrameDecoder.java:469) at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:404) at io.netty.handler.codec.serialization.ObjectDecoder.decode(ObjectDecoder.java:68) at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:351) at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:231) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:131) at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:372) at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:780) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:99) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:497) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:465) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:359) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) at java.lang.Thread.run(Thread.java:745) |
Subscribe to:
Posts (Atom)

