Bitwise Manipulation.
Bitwise Manipulation.
In C programming, bitwise operators are operators that perform bit-level operations on the binary
representations of integers. There are six bitwise operators in C:
Bitwise AND (&) - Performs a logical AND operation on each corresponding pair of bits. The result is 1 if
both bits are 1, otherwise, it is 0.
Bitwise OR (|) - Performs a logical OR operation on each corresponding pair of bits. The result is 1 if at
least one bit
In C programming, bitwise operators are operators that perform bit-level operations on the binary
representations of integers. There are six bitwise operators in C:
Bitwise AND (&) - Performs a logical AND operation on each corresponding pair of bits. The result is 1 if
both bits are 1, otherwise, it is 0.
Bitwise OR (|) - Performs a logical OR operation on each corresponding pair of bits. The result is 1 if at
least one bit is 1, otherwise, it is 0.
Bitwise XOR (^) - Performs a logical XOR operation on each corresponding pair of bits. The result is 1 if
the bits are different, otherwise, it is 0.
Bitwise NOT (~) - Inverts all the bits of a number. If a bit is 0, it becomes 1, and if a bit is 1, it becomes 0.
Left Shift (<<) - Shifts the bits of a number to the left by a specified number of positions. The bits that are
shifted out on the left are discarded, and the empty positions on the right are filled with zeros.
Right Shift (>>) - Shifts the bits of a number to the right by a specified number of positions. The bits that
are shifted out on the right are discarded, and the empty positions on the left are filled with either zeros
or the sign bit (in case of signed integers).
Bitwise operators are important because they can be used to manipulate individual bits in a number,
which can be useful in various scenarios such as bit-packed data structures, low-level hardware
interactions, and performance optimizations.
unsigned char g = a << 2; // Left shift by 2 positions, g is now 0xD8 (11011000 in binary)
unsigned char h = b >> 1; // Right shift by 1 position, h is now 0xD2 (11010010 in binary)
Note that bitwise operators only work on integers, and the behavior of the operators depends on the
size and signedness of the integer type. It's important to understand the binary representation of the
numbers you're working with and the behavior of the operators to avoid unexpected results.
When a character is used with bitwise operators in C, the character is first converted to its
corresponding integer value (using its ASCII code) and the bitwise operation is then performed on the
integer value. After the operation is completed, the result can be converted back to a character if
necessary.
Here's an example:
ch = ch | 0x0F;
In this example, the bitwise OR operation is performed on the integer value of the character 'A' (65 in
decimal, 0x41 in hexadecimal) and the integer value 0x0F (15 in decimal, 00001111 in binary). The result
of the bitwise OR operation is the integer value 79 (0x4F in hexadecimal), which corresponds to the
character 'O' in ASCII.
So, declaring a char variable can be useful when working with character data that needs to be
manipulated using bitwise operations
In C programming, you can convert integers or characters to hexadecimal notation using the %x format
specifier with the printf() or sprintf() functions.
In this example, the %x format specifier is used to print the hexadecimal representation of the num
variable. The output will be
To convert a character to hexadecimal notation, you can cast the character to an integer type and then
use the %x format specifier. Here's an example:
char ch = 'A';
In this example, the ch variable is first cast to an int type to obtain its ASCII code value, and then the %x
format specifier is used to print the hexadecimal representation of the ASCII code. The output will be:
If you want to convert an integer or character to a hexadecimal string (i.e., an array of characters
representing the hexadecimal notation), you can use the sprintf() function with the %x format specifier.
Here's an example
char hex_str[3]; // The array should be large enough to hold the hexadecimal representation plus a null
terminator
In this example, the sprintf() function is used to format the integer num as a hexadecimal string and
store it in the hex_str array. The output will be
Note that when using sprintf(), you need to make sure that the target buffer is large enough to hold the
formatted string and a null terminator. In this example, the hex_str array is declared with a size of 3 to
hold the two hexadecimal digits plus a null terminator.
You know how when converting base 10 numbers to binary i divide the number by 2 and write down
the remainder. is there a shortcut for hexadecimals too
Yes, there is a shortcut to convert decimal numbers to hexadecimal using the successive division and
remainder method, similar to the way you convert decimal numbers to binary by dividing by 2 and
writing down the remainder. The difference is that when converting to hexadecimal, you divide by 16
instead of 2, and the remainders can be any digit from 0 to 15, which corresponds to the digits 0-9 and
A-F in hexadecimal.
Here's an example of how to convert the decimal number 255 to hexadecimal manually using the
successive division and remainder method:
This method can be extended to convert any decimal number to hexadecimal. However, for larger
numbers, it can be tedious to perform the calculations manually, and it's often easier to use a
programming language or a calculator with built-in conversion functions.
0x12
In hexadecimal notation, each digit can represent 16 different values, from 0 to 15. The digit '0x1'
represents one "sixteen" or 16, and the digit '0x2' represents two "ones" or 2, so when combined, the
hexadecimal value 0x12 represents the decimal value 16 + 2 = 18.
The prefix '0x' is used to indicate that the number is written in hexadecimal notation.
convert 89 to base 2
To convert the decimal number 89 to binary, you can use the successive division by 2 method:
89 / 2 = 44 remainder 1
44 / 2 = 22 remainder 0
22 / 2 = 11 remainder 0
11 / 2 = 5 remainder 1
5 / 2 = 2 remainder 1
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
To convert the hexadecimal number 0x89 to binary, you can first convert it to its equivalent decimal
value and then use the successive division by 2 method to convert to binary. Here's how:
137 / 2 = 68 remainder 1
68 / 2 = 34 remainder 0
34 / 2 = 17 remainder 0
17 / 2 = 8 remainder 1
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
Reading the remainders from bottom to top gives the binary representation of 137, which is 10001001.