Chp 14 Operations On Bits Flashcards
To interact with hardware.
Introduction:
So far we smallest element we can operate on is Byte. Being able to operate on bit level is crucial when interacting with hardware.
Prgramming langs are byte oriented whereas hardware tends to be bit oriented.
Bitwise Operators
dont list them:
These permit access and manipulate individual bits within a piece of data.
Operate upon int and char, not on float and doubles.
How are bits numbered:
They numbered zero onwards, increasing from right to left.
7 6 5 4 3 2 1 0
a byte.
BITWISE OPERATORS LIST
6
~ 1's complement >> Right Shift << Left Shift & Bitwise And | Bitwise OR ^ Bitwise XOR
A function, to find out binary form of int,char.
showbits()
displays the binary representation of any integer or character value.
One’s Complement Operator:
use ish something..
~
1 operand
Changes all 1’s to 0’s and all 0’s to 1’s.
Can be used for encryption.
but what happens to EOF, ASCII value 26?
(even that will have some binary representation )
{found 2’s complement of 26 is -26. Maybe bullshit, verify.}
Right Shift Operator:
ch»_space; 1
Needs two operands.
Shifts each bit to right [ in left operand]
right operand determines to shift by how many bits.
The space on left is filled with xeros
If left operand is multiple of 2 then shifting it by 1 bit is equal to dividing it by 2 and ignoring the remainder.
if right operand is negative then result is unpredictable.
if left is -ve, then it will have leftmost bit as 1 (sign bit), that 1 bit gets extended in author’s machine so -1»4 is 1111 1111 1111 1111 and not 0000 1111 1111 1111
Left Shift operator:
«
Shifts each bit to left and fills blanks on right with 0’s.
How is date of creation(modification) stored in Windows/DOS ?
The date on which a file is created or modified is stored as 2 byte entry in the 32 byte directory entry of that file. that entry is codified.
The formula used for it:
512*(year - 1980) + (32 * month ) + day.
the resulting number is then converted to binary and stores in 2 bytes.
( 2 raise to 9 = 512, 2 raise to 5 = 32 )
YYYY YYYM MMMD DDDD - 2 byte distribution of YMD.
now MMMM bits decimal value will give the month and so on.
How is the date displayed back in mm/dd/yy
and stored in MSDOS:
It is stored as 2 byte entry.
YYYY YYYM MMMD DDDD is converted to mm/dd/yy by using left shift and right shift.
to get year we right shift by 9,[1980 + (date»_space;9)];
to get a month we left shift by 7 and then right shift by 12.( (date«7)»_space; 12);
to get day we left shift by 11 and right shift by 11.
( (date «11)»_space; 11 );
Bitwise AND operator:
&
Works with 2 operands, these are compared bit by bit, so they must be of same type.
Second operand is called AND MASK.
0&0 = 0, 0&1 = 0, 1&1 = 1.
Application of & :
- To check if ith bit is on (1) or off(0).
then and mask = 2raise to i
If resulting bit is all 0 then its off else its on. - In every file entry present in directory there is an attribute byte, which can tell us the status of the file.
0th bit : Read only
1st : Hidden
2nd : System
3rd : Volume label entry
4th : Sub - directory entry
5th : Archive bit
6th , 7th : Unused.
We can use appropriate and mask to find out the status. - To switch off ith bit, we use and mask whose ith bit is 0 rest all 1.
Bitwise OR operator and its use:
|
0 | 0 = 0, 0 | 1 = 1, 1 | 1 = 1
second operand is OR MASK
Used to switch on ith bit, take or mask as ith bit 1 and rest all 0.
Bitwise XOR:
and uses:
Exclusive OR operator.
returns 1 for diff bits, returns 0 for same bits. [ 2 bits operation ]
- toggle a bit on or off.
- A number XORed with another number twice returns same number.
showbits()
showbits( int n) { int i, k, andmask; for(i = 15; i>=0; i --) { andmask = 1 << i; k = n & andmask; k = 0 ? printf('0'): printf('1'); } } Note andmask in every loop has 1 on position i. k is 0 for ith bit being 0 or else 1 for being 1.