You are on page 1of 6

what are bitwise operators in c

why are they important

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

what are bitwise operators in c

why are they important

how do i use them

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.

Here's an example of how to use bitwise operators in C

unsigned char a = 0x5A; // 01011010 in binary

unsigned char b = 0xA5; // 10100101 in binary

unsigned char c = a & b; // Bitwise AND, c is now 0x00 (00000000 in binary)

unsigned char d = a | b; // Bitwise OR, d is now 0xFF (11111111 in binary)

unsigned char e = a ^ b; // Bitwise XOR, e is now 0xFF (11111111 in binary)

unsigned char f = ~a; // Bitwise NOT, f is now 0xA5 (10100101 in binary)

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.

why declare a char if bitwise operators only work on integers


Although bitwise operators work on integers, it's also possible to use them with characters in C. This is
because characters in C are represented as integers in memory, with each character having a
corresponding ASCII code value.

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:

char ch = 'A'; // ASCII code for 'A' is 65 (0x41 in hexadecimal)

// Perform a bitwise OR with 0x0F (00001111 in binary)

ch = ch | 0x0F;

// The result of the bitwise operation is now 79 (0x4F in hexadecimal)

// which corresponds to the character 'O'

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

how do i convert integers or characters to hexadecimals

In C programming, you can convert integers or characters to hexadecimal notation using the %x format
specifier with the printf() or sprintf() functions.

Here's an example of how to convert an integer to hexadecimal using printf():


int num = 255;

printf("The hexadecimal representation of %d is %x\n", num, num);

In this example, the %x format specifier is used to print the hexadecimal representation of the num
variable. The output will be

The hexadecimal representation of 255 is ff

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';

printf("The hexadecimal representation of '%c' is %x\n", ch, (int) ch);

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:

The hexadecimal representation of 'A' is 41

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

int num = 255;

char hex_str[3]; // The array should be large enough to hold the hexadecimal representation plus a null
terminator

sprintf(hex_str, "%x", num);

printf("The hexadecimal representation of %d is %s\n", num, hex_str);

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

The hexadecimal representation of 255 is ff

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:

Divide 255 by 16 and write down the remainder: 15 (which is F in hexadecimal)

Divide 15 by 16 and write down the remainder: 15 (which is also F in hexadecimal)

The final hexadecimal representation of 255 is: FF

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.

what is this hexadecimal as an integer

0x12

The hexadecimal value 0x12 represents the integer value 18 in decimal.

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

what about converting 0x89 to base 2

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:

0x89 = 8 * 16^1 + 9 * 16^0 = 137

Now, to convert 137 to binary

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.

Therefore, the binary representation of the hexadecimal number 0x89 is 10001001

You might also like