So in this article we will discuss something obvious to experts and insidious for beginners. The elementary Software-Hardware interaction In Embedded, the elementary software-hardware interaction is performed writing/reading registers. In digital electronics registers are special structures storing bits of information in such a way that systems can write to or read out all the bits simultaneously.
They could be used to make status reporting (changing their value whether a certain event has occurred), as input/output or to configure certain features. Configuring a peripheral by software always means write/read one or more bit in a register.
Software targeted on a specific platform could often encapsulate these operations hiding them to the user. When software is cross-platform or when user needs to change the hardware configurations at run time, acting on registers is almost mandatory.
Case analysis The Baud rate control field from an SPI CR. Basically, configuring that register from the beginning means compose the entire value bit after bit and write value on it. But if register is somehow already configured, and we need to change only some bit -fields of it things become interesting.
As example, lets take a look to some SPI_CR1 bit -field descriptions to figure out what are the elementary operation that typically we need to perform dealing with registers. For example C uses an integer type, where relational expressions like i < j and logical expressions connected by & and | | are defined to have value 1 if true and 0 if false, whereas the test parts of if, while, for, etc., treat any non-zero value as true.
Using logical operators, operands are considered always like boolean and the operation return another boolean: using other words doesn’t matter the single bit value. Using certain operand in a single bitwise operation we can set to ‘1’, to ‘0’ or toggle multiple bits in a word.
It is clearly hard to understand what kind of configuration we made even for ourselves. If you have read my other posts today, then, you probably know that I have started my YouTube channel for tutorials related to competitive programming.
In this post, I am going to introduce you to “basics of Bit Masking“. This part is just to introduce you to concept of Bit Masking.
Binary digits follow the analogy with Boolean algebra in the sense that “1” represents true and “0” represents false. All those operations we can perform on Boolean are also applicable on binary digits.
OR (|) ‘|’ returns true if at least one of the operands is true which means that it returns false when both the operands are false. Beyond these 4, we can also perform following 2 operations on integers in binary representation.
Left Shift Operator (a << b) When applied on integer a, it will append a ‘0s’ in the end of binary representation of that integer. Right Shift Operator (a >> b) It will remove b rightmost bits from the integer a.
Check whether a number is odd or not As all of you know that a binary number of form BCD is represented as a×2 3 + b×2 2 + c×2 1 + d in decimal system. Clearly, all the term in binary to decimal expansion sum are even except for d.
So, whether a number is even or odd depends upon the rightmost bit. If the rightmost bit is 1, then, the number is odd and otherwise even.
Although we can convert the number to binary and then, can easily find the with bit, but it will take O(log 2 n) time, and we want to do it in O(1) time. Note that we are following 0 based a bit indexing.
Hence, we can set with bit by taking its ‘|’ with 1. Now, it’s time to get into the real deal and the main topic of this tutorial.
Before telling, you what is a bit Masking, let me tell you that it is not an algorithm like merge sort and not any problem-solving technique like dynamic programming. Similarly, it is not in the set if with bit of integer is unset (0).
And we have already discussed that setting and unsetting bits in an integer takes O(1) time and hence, we can add and remove elements from the set in O(1) time. If we consider the time factors, then, size of ideal array should not be more than 20.
So, if you see any problem with small constraints in any contest, then, there is high probability that it will be solved using a bit Masking. One thing which I have not told you are that it can make implementation in many cases a lot easier.
Subset sum problem Given an array A of size n < 20 of integers, the task is to print sum of all the subsets of this array. Solution It can be easily implemented recursively but if it comes to iterative implementation, then, anybody will shit in their pants if they don’t know a bit Masking.
So, to find sum of each subset, we will simply iterate from 0 to 2 n and find and print the sum of each subset. I think that’s enough of Bit Masking introduction for today.
But don’t worry I have just started and in coming tutorials, I will discuss a bit Masking with dynamic programming. If you liked this tutorial, then, please follow my blog and share it with your friends.
Meanwhile, don’t forget to solve problem on hacker earth. You can subscribe to my YouTube channel for video tutorials on competitive programming.
In computing, numbers are internally represented in binary. This means, where you use an integer type for a variable, this will actually be represented internally as a summation of zeros and ones.
A concatenation of eight of those bits represent a Byte, e.g. 00000101, which is the number 5. This means that your number actually uses 32 bits of the internal storage.
But for simplicity reasons, throughout this answer I will use 8-bit numbers. Now imagine you want to create a program that holds a state, which is based on multiple values that are one(true) or zero(false).
One could store these values in different variables, may they be Boolean or integers. Here the first bit (reading from right to left) is true, which represents the first variable.
To turn certain bits on, the bitwise OR operation can be used, following the principle that Y OR 1 = 1 and Y OR 0 = Y. To do this, turning off all the other bits using the bitwise AND is done as discussed above and the value is compared with 0.
What makes this convenient is that it is not necessary to figure out what the value actually is, just that it is not 0. XOR returns 1 if and only if an odd number of bits are 1.
Therefore, inversion of the values of bits is done by XOR ING them with a 1. In the SVG file, click a card to toggle it In programming languages such as C, bit fields are a useful way to pass a set of named boolean arguments to a function.
For example, in the graphics API OpenGL, there is a command, clear() which clears the screen or other buffers. Internally, a function taking a bit field like this can use binary and to extract the individual bits.
The advantage to this approach is that function argument overhead is decreased. Since the minimum datum size is one byte, separating the options into separate arguments would be wasting seven bits per argument and would occupy more stack space.
To configure IP addresses on interfaces, masks start with 255 and have the large values on the left side: for example, IP address 220.127.116.11 with a 255.255.255.224 mask. When the value of the mask is broken down into binary (0s and 1s), the results determine which address bits are to be considered in processing the traffic.
A 0 indicates that the address bits must be considered (exact match); a 1 in the mask is a “don't care”. Therefore, all traffic that begins with 10.1.1. Matches since the last octet is “don't care”.
In computer graphics, when a given image is intended to be placed over a background, the transparent areas can be specified through a binary mask. This way, for each intended image there are actually two bitmaps : the actual image, in which the unused areas are given a pixel value with all bits set to 0s, and an additional mask, in which the correspondent image areas are given a pixel value of all bits set to 0s and the surrounding areas a value of all bits set to 1s.
At run time, to put the image on the screen over the background, the program first masks the screen pixel's bits with the image mask at the desired coordinates using the bitwise AND operation. This preserves the background pixels of the transparent areas while resets with zeros the bits of the pixels which will be obscured by the overlapped image.
The result is a perfect compound of the image over the background. This technique is used for painting pointing device cursors, in typical 2-D video games for characters, bullets and so on (the sprites), for GUI icons, and for video titling and other image mixing applications.
Although related (due to being used for the same purposes), and alpha channels are techniques which do not involve the image pixel mirage by binary masking.