Chp 14 Operations On Bits Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

To interact with hardware.

Introduction:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Bitwise Operators

dont list them:

A

These permit access and manipulate individual bits within a piece of data.
Operate upon int and char, not on float and doubles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are bits numbered:

A

They numbered zero onwards, increasing from right to left.
7 6 5 4 3 2 1 0
a byte.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

BITWISE OPERATORS LIST

6

A
~               1's complement
>>             Right Shift
<<             Left Shift
&amp;               Bitwise And
|                 Bitwise OR
^               Bitwise XOR
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A function, to find out binary form of int,char.

A

showbits()

displays the binary representation of any integer or character value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

One’s Complement Operator:

use ish something..

A

~
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.}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Right Shift Operator:

A

ch&raquo_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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Left Shift operator:

A

«

Shifts each bit to left and fills blanks on right with 0’s.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is date of creation(modification) stored in Windows/DOS ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is the date displayed back in mm/dd/yy

and stored in MSDOS:

A

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&raquo_space;9)];
to get a month we left shift by 7 and then right shift by 12.( (date«7)&raquo_space; 12);
to get day we left shift by 11 and right shift by 11.
( (date «11)&raquo_space; 11 );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Bitwise AND operator:

A

&
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Application of & :

A
  1. 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.
  2. 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.
  3. To switch off ith bit, we use and mask whose ith bit is 0 rest all 1.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Bitwise OR operator and its use:

A

|
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Bitwise XOR:

and uses:

A

Exclusive OR operator.
returns 1 for diff bits, returns 0 for same bits. [ 2 bits operation ]

  1. toggle a bit on or off.
    - A number XORed with another number twice returns same number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

showbits()

A
showbits( int n)
{
int i, k, andmask;
for(i = 15; i>=0; i --)
{   andmask = 1 << i;
    k = n &amp; 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly