0% found this document useful (0 votes)
58 views

6 - OperatorExamples, BitWise Operators

The document provides information on binary, octal, and hexadecimal number systems including how to convert between them. It also discusses bitwise operators like AND, OR, XOR, NOT, left and right shift and provides examples of their usage. Finally, it covers bitwise operations to test and manipulate bits of a number.

Uploaded by

Avi Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

6 - OperatorExamples, BitWise Operators

The document provides information on binary, octal, and hexadecimal number systems including how to convert between them. It also discusses bitwise operators like AND, OR, XOR, NOT, left and right shift and provides examples of their usage. Finally, it covers bitwise operations to test and manipulate bits of a number.

Uploaded by

Avi Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Fractional

Decimal to Binary

Example: number is (98.46)10 in decimal


Decimal to binary
(1100010.0111)2
Binary to Decimal

Fractional binary to Decimal


Binary to Octal Octal to Binary

Fractional Octal to Binary


22+ 21 + 20
4 2 1
0 0 0 0
0 0 1 1
0 1 0 2
0 1 1 3
1 0 04
1 0 1 5
1 1 06
1 1 1 7
23 +22+ 21 + 20
8 4 2 1
0 0 0 0 0
0 0 0 1 1
0 0 1 0 2
0 0 1 1 3
0 1 0 0 4
0 1 0 1 5
0 1 1 0 6
0 1 1 1 7
1 0 0 0 8
1 0 0 1 9
1 0 1 0 10 A
1 0 1 1 11 B
1 1 0 0 12 C
1 1 0 1 13D
1 1 1 0 14 E
1 1 1 1 15 F
Bitwise operators ( &, |, ^, ~, <<, >> )
Bitwise operators modify variables considering the bit patterns that represent the values they store.

operator asm equivalent description


& AND Bitwise AND
| OR Bitwise inclusive OR
^ XOR Bitwise exclusive OR
Unary complement (bit
~ NOT
inversion)
<< SHL Shift bits left
>> SHR Shift bits right
Let a = 5, b = 9
1. a&b = 1
2. a|b = 13
3. a^b = 12 & (AND) | (OR)
4. b<<1 = 18 5- 00000101 5- 00000101
5. b>>1 = 4 9- 00001001 9- 00001001
1- 00000001 13-00001101
^ (XOR)
5- 00000101 << Left shift >> Right shift
9- 00001001 9- 00001001 9- 00001001
12-00001100 18-00010010 4-00000100
Bitwise Not (~)
— Tilde symbol (~) is used for bitwise NOT
a=2;
c=~a;
c=-3
To solve this You will first convert the value of a that is 2 into binary.
2 in binary is 00000010
You may consider 32 bits also
…..000000000010
(To keep things simple, I am just assuming 8 bits)

Now ~a will be (invert the bits)


11111101
You might think answer is the decimal representation of this value !! But its not

Why?? Because if you would have considered 16 bits then answer would have been different !!
And if you would have considered 32 bits then answer would have been different !!
So what would the answer be??
Bitwise Not in this case gives you a negative number actually 11111101
See in 11111101, Most significant bit is 1, that means a negative number.
So to know the answer, keep the MSB intact and reverse all other bits à you
get 10000010

Now Add 1 to this à 10000010


+1
10000011
required answer.
Here Most significant bit 1 means – (minus) and rest bits are giving the value
So answer is -3
Question
Consider val=0xCAFE; Write expressions using bitwise operators
that do the following:
1) test if atleast three of last four bits (LSB) are on (1)

Solution:
Here 0xCAFE means hexadecimal number CAFÉ.
(In Hexadecimal number system, 0-9 are represented as such and 10 is represented as
A, 11 as B, 12 as C, 13 as D, 14 as E, 15 as F

Now convert CAFÉ into its binary representation

1100 1010 1111 1110

NOTE: Every hexadecimal number must precede with 0x


1100 1010 1111 1110 - our given number in binary
test if atleast three of last four bits (LSB) are on (i.e true or 1)
To test this, we only need last 4 bits of the number i.e 1110.
So there are 5 cases where atleast three of last 4 bits are on à 1110 (0xE),
0111 (0x7), 1011 (0xB), 1101 (0xD), 1111 (0xF)

If our input number i.e CAFÉ falls into any of these cases, then the test
condition will be satisfied.

But our number contains 8 other bits also apart from last four, so we need to
make them 0 so that we are left with the last 4 bits only to compare. So we
first take bitwise and (&) of 0xCAFE with 0xF (0x000F)

1100 1010 1111 1110


&
0000 0000 0000 1111
0000 0000 0000 1110 (0xE)

Now we are only left with last 4 bits, we can easily check these if they fall into any of the 5 cases
using if condition
Consider int val=0xCAFE; Write expressions using bitwise operators
that do the following:
test if atleast three of last four bits (LSB) are on

val=0xCAFE;
bits=val & 0xF;

if (bits==0x7) or (bits==0xE) or (bits==0xB) or (bits==0xD) or (bits==0xF):


print("atleast three of last four bits (LSB) are on");

else:
print("atleast three of last four bits are not on");
Consider val=0xCAFE; Write expressions using bitwise operators that
do the following:
1) test if atleast three of last four bits (LSB) are on
2) reverse the byte order (i.e., produce val=0xFECA)
3) rotate four bits (i.e., produce val=0xECAF)

val1 = ((0x00FF & val) << 8) | (val>>8);

0xCAFE 1100 1010 1111 1110


& 0x00FF 0000 0000 1111 1111

0x00FE 0000 0000 0000 0000 1111 1110 <<8


0xCAFE 1100 1010 1111 1110 >>8
0000 0000 1100 1010 0x00CA
0xFE00 1111 1110 0000 0000
|
0xFECA
val=0xCAFE
val1 = ((0x00FF & val) << 8) | (val>>8);

Lets understand this line

Here we have taken & of our input number 0xCAFE (which was put in val variable) with
0xFF why are we taking this AND? Because if we directly apply
1100 1010 1111 1110 CAFE left or right shift in input number, some bits will become 0
0000 0000 1111 1111 & and actual value will be tampered
0000 0000 1111 1110

Now apply left shift by 8 positions (<<8) in this resulting bits. You will get --
1111 1110 0000 0000 ----------(1)

Apply right shift in the input number val by 8 (val>>8)


0000 0000 1100 1010 -----------(2)

Now take bitwise OR between (1) and (2). You get


1111 1110 1100 1010 (required answer) FECA
Consider int val=0xCAFE; Write expressions using bitwise operators
that do the following:
1) test if atleast three of last four bits (LSB) are on
2) reverse the byte order (i.e., produce val=0xFECA)
3) rotate four bits (i.e., produce val=0xECAF) 0000000000001110
1110000000000000

val1 = ((val&0xF)<<12)|(val >> 4)

CAFÉ 000F &


000E <<12
E000 0CAF

ECAF
In-Built functions
— hex()
— oct()
— [ord(c) for c in "CAFE"] #to get the ASCII values
output: [67, 65, 70, 69]
— chr(67)
output: “C”
— (0xCAFE).bit_length()
Output: 16

Write import this in IDLE and see what happens….

You might also like