Sie sind auf Seite 1von 6

A. What is a Computer Word?

Bit- Vector
- is an array data structure that compactly stores bits. It can be used to implement a
simple set data structure. A bit array is effective at exploiting bit-level parallelism in
hardware to perform operations quickly. A typical bit array stores kw bits, where w is
the number of bits in the unit of storage, such as a byte or word, and k is some
nonnegative integer. If w does not divide the number of bits to be stored, some space is
wasted due to internal fragmentation.
Bit-manipulation operations
Bitwise Logical Operators
bit b

a&b

a|b

bit a

a^b

Precedence : << >> & ^ |


o Bitwise OR ( | )
- Perform OR operations with 2 bit patterns.
Ex: 00110011 (51)
| 00010001 (17)
-------------0 0110011 (51)
o Bitwise AND ( & )
- Perform AND operations with 2 bit patterns.
Ex: 00110011 (51)
& 01010101 (85)
-------------00010001 (17)

o Bitwise XOR ( ^ )
- Perform XOR operations with 2 bit patterns.
Ex:
00110011 (51)
^ 01010101 (85)
-------------0 1100110 (102)
o Complement Operator ( ~ ) change 1 with 0, and vice versa.
Ones Compliment
Ex: ~ 0001111
-------------1110000
Twos Compliment
Ex: ~ 0001111
-------------1110000
+
1
------------1111001
Shift Operators
Logical Shifting always pad zero
Left Shift ( << )
Ex: 01101100 (108<<3)
01100000 (96)
Right Shift ( >> )
Ex: 11000000 (192>>3)
00011000 (24)
Arithmetic Shifting pad zero in Left Shift and pad 1 in right.
Left Shift ( << )
Ex: 01101100 (108<<3)
01100000 (96)
Right Shift ( >> )
Ex: 11000000 (192>>3)
11111000 (248)

B. Given an integer, write the code, of the function, that will display the bit-pattern using bitwise, relational, logical operators only. If error occurs, what is the error and how did you fix it.
Code:
void BitPattern (int num)
{
int flag=1, x;
int bit[8] = {0};
for(x = 0;x < 8 ;x++)
{
if(num&flag) // make 1 to be flag to check the bit of num
{
bit[x]=1;
}
else
{
bit[x]=0;
}
flag = flag<<1; // move the 1 left shift to check all digits
}
printf("\nBit pattern: ");
for(x = 7 ;x >=0;x--)
{
printf("%d",bit[x]); //printing array in reverse order
}
}
Result:

Errors:
[Error] logical, cant print the bit pattern
[Solution] (num&flag)==1, changed to: (num&flag)
[Error] logical, the values are printed backwards.
[Solution] instead of initializing the x in 2nd for loop to 0, its change to 7
[Error] logical, result in trial: num=28 = 00000000, and all numbers = 00000000
[Solution] flag<<1 was changed to flag=flag<<1

C. Find a PhilNITS question that uses that concept of Computer Word/ Bit manipulation
Solve: 2007 October FE AM Q1
Problem:
What range of decimal numbers can be represented with an 11-bit twos complement
number?
Choices:
a) -2048 to 2047
b) -2048 to 2048
c) -1024 to 1023
d) -1024 to 1024
Solution:
In getting the decimal value of bit pattern the most significant number is to be
considered a sign bit.
In bit pattern with 11bits :
The highest positive bit pattern will be 01111111111 (zero representing positivity) = 1023
The lowest negative bit pattern will be 10000000000 (one representing negativity) = -1024

The lowest number which can be represented is 100 0000 00002 = -1024.
If the leftmost bit is a 1, the number is negative.
a. Flip the bits (1 becomes 0, 0 becomes 1
b. Add 1 to the number.
c. Report its negation.
10000000000
01111111111
+
1
-----------------10000000000 = -1024

Das könnte Ihnen auch gefallen