The C Shift-left and Shift-right Bitwise Operators
This is the continuation of the C programming tutorial series from the last post which I wrote Dec 7 of last year.
The shift-left operator (<<, or double left) is used to shift the bit pattern of a number by a certain number of bits to the left.
The shift-right operator (>>, or double right) is used to shift this bit pattern to the opposite direction (right).
For example, the bit pattern of five (5) is 101. The result bit pattern of shifting it 4 bits to the left is 1010000.
If shifting left are too far such as 50, it will produce undefined results, while right shifting too far will simply be truncated.
Here is an example of shift-left operator:
13<<3=104 13 (00001101) 104 (01101000)
The bit pattern between 13 and 104 is exactly shifted left 3 places.
Notice the division of 104 and 13 is exactly 8, which is 2 to the power of 3, which means
13*2^3=104
which is almost the same as the above statement with shift-left operator.
Here is another example for the shift-right operator:
52>>4=3 52 (00110100) 3 (00000011)
The bit pattern between 52 and 3 is shifted right 4 spaces, which removed the last 4 digit (0100) which is the reminder of 3.
This is exactly the same as the last example except this one is divide by 2 to the power of 4:
52 / 2^4 = 3
Note that this is integer division, therefore there is no remainder or decimal.
Based on the above examples, you can see that shifting left is multiply by 2 and sifting right is divide by 2.
Below is the example code for the shift operators:
/*
* shift.c
*
* Program to demonstrate the shift left and right operators
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
#include <stdlib.h>
static void goodbye() {
while (getchar() != 'n') {};
printf("nPress ENTER to exit: ");
fflush(stdin);
getchar();
}
main() {
unsigned int number;
int shift;
unsigned int result;
atexit(goodbye);
printf("Please enter a number: ");
scanf("%d", &number);
printf("How many bits would you like to shift (negative to shift right)? ");
scanf("%d", &shift);
if (shift >= 0) {
result = number << shift;
}
else {
result = number >> -shift;
}
printf("%u (hex %X) shifted %s %d is %u (hex %X)n",
number, number,
shift >= 0 ? "left" : "right",
shift >= 0 ? shift : -shift,
result, result);
return 0;
}
If you have any issue compiling the above code or any question regarding this post, leave a comment below and I will help you ASAP.
C Bitwise Operators
Introduction
The bitwise operators are used to manipulate the individual bits.
About Bits
Any type of integer number (int, long, short) is a series of bits. For example, long has an array of 32 bits.
These bits represent the number in binary arithmetic or base-2, which I think it’s the machine code representation which are translated to 0 and 1. For example, the decimal number 87 can be represented in base-2 as 1010111.
Note that since the base-2 numbers only have 0 and 1 and these two numbers also represent boolean values, the bits can be represented as individual boolean values. And they are the purpose of bitwise operators – to access the boolean values from the integers.
Video
Here is the video which this post is based on:
Sorry about the quality of the video. I suppose it’s because it’s in .mov format, but I’m glade that it successfully converted to Flash video. It doesn’t play properly in all the players I’ve tried other than Totem Movie Player. It can’t even convert properly using FFmpeg.
Data Types and Data Type Modifiers for C Programming Language
Below are the fundamental (or basic) data types of C:
- int – used for storing integers
- char – used for storing individual characters
- float – used for storing floating point of decimal numbers
- double – used for storing more decimal points than float
- void – special data type only for functions and pointers
When I was learning C++ two years ago, I couldn’t differentiate between float and double. After countless examples that I saw today, I finally understand the difference between these two. As I defined above, the float data type is used to storing short decimal points, like price which has two decimal points. The double data type is used to storing long decimal points, like pi or infinite decimal points.
The data type modifiers that I learned today are following:
- short, long – used only for int (integers), to change size of the data type for save memory space
- unsigned, signed – also used only for int. Unsigned modifies int to positive number range. Signed is the default modifier for all of the data types.
- There are more modifiers, but I only learned these two groups today.
Since these modifiers only work for integer data type, int can be removed when declaring the variable with the modifiers. For example:
int age;Â Â Â Â Â Â Â Â Â Â Â // No data type modifiers long age;Â Â Â Â Â Â Â Â Â Â // With modifiers unsigned age;Â // With modifiers
Variable Rules – C vs PHP
Here is my first post about C programming language which I decided to learn this summer. Today I compare the variable declaration rules between C and PHP. The main difference between C and PHP’s variable declaration process is that C’s variable name must be declared at the beginning of the code as well as its type, whereas PHP creates variables on the fly without indicating variable type. In C, variable name must be declared uniquely within the current code, except the same name can be declared inside the functions. This is different from PHP. Thanks to its dynamic variable type, a variable name can be used as a number and a string depending what’s the value inside the variable. The variable naming rules for C is the same as PHP. It also doesn’t allow to place digits first of the variable name. I also learned that the length of a variable name in C should not be longer than 31 characters. I never heard of this rule in PHP. However, I never used more than 10 characters for the PHP variable names. I will try to apply my 10-character policy to C once I have a real C programming project. I’m looking forward to programming with C and play around with some open source code when I understand C programming language a little more.
Starting to learn C Programming Language
I have decided to learn a new programming language during this summer break. I chose C because I have already learned the basics of C++. The C++ programming language is the object oriented version of C after all. Another reason I chose to learn C because the source code for many of the open source projects that I’ve read are written in C language. It seems that the language is very popular in open source community along with C++ and Java. I’m starting with VTC C Programming video tutorials. This is the first video tutorials that I came across when I was searching for C programming language tutorials online. Therefore I downloaded it and am beginning to watch and learn C language. I will post any tips that inspired by the tutorials as I learn instead of post what I learned from the tutorials.
Here are the ISO images of this video tutorial series if you are a copylefter or don’t care about the copyright:
http://www.downtr.net/162643-vtc-c-coding-video-tutorial.html