Introduction to c



Download 0.91 Mb.
Page12/13
Date28.05.2018
Size0.91 Mb.
#51782
1   ...   5   6   7   8   9   10   11   12   13

Bitwise Shift Operators


The shift operators are used to move bit pattern either to the left or to the right and they require two operands to operate. The first one is an integer type operand that represents the pattern to be shifted. The second is an unsigned integer that indicates the number of displacement (i.e., the number of positions to be shifted). The different shift operators are,

  • Right shift operator

  • Left shift operator

Right Shift Operator


The right shift operator is represented by the symbol, . The right shift operator causes all the bits in the first operand to be shifted to the right by the number of positions indicated by the second operand. The rightmost bits in the original bit pattern will be lost. If the bit pattern shifted represents an unsigned integer, then the leftmost bit positions that become vacant will be filled with zeros. If the bit pattern representing a signed integer is shifted to the right, the outcome of the shift operation may depend on the value of the leftmost bit (the sign bit). Most compilers will fill the vacant bit positions with the contents of this bit. Some compilers will fill the vacant bit positions with zeros regardless of the sign of the original integer quantity.
E.g. x 0000 0000 0000 1000 ( decimal 8 )

x = x  2;

x  0000 0000 0000 0010 ( decimal 2 )
Shifting the operand one bit to the right is same as dividing it by 2.
main()

{

int x=50, y=40;



printf(“ x = %d \t y = %d \n”, x, y); /* will print x = 50 y = 40 */

x = x  1;

y = y  1;

printf(“ x = %d \t y = %d \n”, x, y); /* will print x = 25 y = 20 */



x = x  1;

y = y  1;

printf(“ x = %d \t y = %d \n”, x, y); /* will print x = 12 y = 10 */

}

Left Shift Operator


The left shift operator is represented by the symbol, . The left shift operator causes all the bits in the first operand to be shifted to the left by the number of positions indicated by the second operand. The leftmost bits in the original bit pattern will be lost, then the rightmost bit positions that become vacant will be filled with zeros.
E.g. x 0000 0000 0000 1000 ( decimal 8 )

x = x  2;

x  0000 0000 0010 0000 ( decimal 32 )
Shifting the operand one bit to the left is same as multiplying it by 2.
main()

{

int x=5, y=4;



printf(“ x = %d \t y = %d \n”, x, y); /* will print x = 5 y = 4 */

x = x  1;

y = y  1;

printf(“ x = %d \t y = %d \n”, x, y); /* will print x = 10 y = 8 */



x = x  1;

y = y  1;

printf(“ x = %d \t y = %d \n”, x, y); /* will print x = 20 y = 16 */

}

Q). Write a function to display the bit pattern of the integer accepted from the user.
Ans:

main() tobinary(int n)



{ {

int x; int bits=15,i,k,andmask;

for(i=bits; i>=0; i--)

printf( “ enter a Number : “); {

scanf(“%d”, &x); andmask = 1  i;

k = n & andmask;

tobinary(x); k == 0 ? printf("0"):printf("1");

}

getch(); printf("\n");



} }

Masking


Masking is a process in which a given bit pattern is transformed into another bit pattern by means of a Logical Bitwise Operation. The original bit pattern is one of the operand, in the bitwise operation. The second bit pattern, which is a specially selected bit pattern that brings out the required transformation to the original is called the mask.

Masking is used in many different ways,



  • To decide (set, clear or toggle) bit pattern of an integer

  • To copy a portion of a given bit pattern to a new variable, while the remainder of the new variable is filled with zeros. ( using bitwise AND)

E.g. n = 0010 0100 1010 1100 &

mask = 0000 0000 1111 1111

n & mask = 0000 0000 1010 1100



  • To copy a portion of a given bit pattern to a new variable, while the remainder of the new variable is filled with ones. ( using bitwise OR)

E.g. n = 0010 0100 1010 1100 |

mask = 1111 1111 0000 0000

n | mask = 1111 1111 1010 1100



  • To copy a portion of a given bit pattern to a new variable, while the remainder of the original bit pattern is inverted within the new variable. ( using bitwise XOR)

E.g. n = 0010 0100 1010 1100 ^



mask = 1111 1111 0000 0000

n ^ mask = 1101 1011 1010 1100


NOTE:

Use & with mask bit = 0 to clear a bit to 0.

Use & with mask bit = 1 to leave a bit unchanged.

Use | with mask bit = 1 to set a bit to 1.

Use | with mask bit = 0 to leave a bit unchanged.

Use ^ with mask bit = 1 to invert a bit.

Use ^ with mask bit = 0 to leave a bit unchanged.

Sample Questions


  1. DOS keeps the date on which a file is created or last modified is stored as a 2-byte entry in the 32-byte directory entry of that file. Thereby saving the valuable 6 bytes for each file. This date is stored in a bitwise method as in the following picture.




15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

Y

Y

Y

Y

Y

Y

Y

M

M

M

M

D

D

D

D

D

Year

Month

Day

DOS converts the actual date into a 2-byte value using the following formula:



Date = 512 *(year – 1980) + 32 * month + day

And then stores the binary equivalent of that obtained number as a 2-byte bit pattern.


Q) Write a program to decode the date field in directory entry using the bitwise operators?

Ans:

# include

# include

# include


main()

{

union REGS i,o;



int handle;

unsigned int d,t;

handle=open("c:\\command.com",O_BINARY);

clrscr();

i.h.al=0;

i.h.ah=0x57;

i.x.bx=handle;

int86(33,&i,&o);

close(handle);

printf("File name : Command.Com\n\n");

d=o.x.dx;

printf("Date in coded format : %u\n",d);

printf("Date in binary format : ");
tobinary(d); /* YYYYYYYMMMMDDDDD */
printf("\nYear\t%u\n",1980+(d>>9) );

printf("Month\t%02u\n",((d<<7)>>12) );

printf("Day\t%02u\n",((d<<11)>>11) );
getch();

}
tobinary(unsigned int n)

{

int i,k,andmask;



for(i=15; i>=0; i--)

{

andmask = 1 << i;



k = n & andmask;

k == 0 ? printf("0"):printf("1");

}

printf("\n");



}
Similarly DOS uses a 2-byte bit pattern to store the time.


15

14

13

12

11

10

9

8

7

6

5

4

3

2

1

0

H

H

H

H

H

M

M

M

M

M

M

S

S

S

S

S

Hour

Minute

second


Q) Write a program to decode the time field in directory entry using the bitwise operators?

Ans:

# include

# include

# include


main()

{

union REGS i,o;



int handle;

unsigned int d,t;

handle=open("c:\\command.com",O_BINARY);

clrscr();

i.h.al=0;

i.h.ah=0x57;

i.x.bx=handle;

int86(33,&i,&o);

close(handle);

printf("File name : Command.Com\n\n");


t=o.x.cx;

printf("\nTime in coded format : %u\n",t);

printf("Time in binary format : ");

tobinary(t); /* HHHHHMMMMMMSSSSS */


printf("\nHour\t%u\n", (t>>11) );

printf("Minute\t%u\n",((t<<5)>>10) );

printf("Second\t%u\n",((t<<11)>>11) );
getch();

}
tobinary(unsigned int n)

{

int i,k,andmask;



for(i=15; i>=0; i--)

{

andmask = 1 << i;



k = n & andmask;

k == 0 ? printf("0"):printf("1");



}

printf("\n");



}

  1. DOS stores the file attributes in a single byte as shown below.




Bit numbers

Meaning

7

6

5

4

3

2

1

0






















1

Read only



















1




Hidden
















1







System













1










Volume label entry










1













Sub directory entry







1
















Archive bit




1



















Unused

1






















Unused


Q) Write a program to display the current attributes of any file.

Ans:

# include

# include
main()

{

unsigned int x;



union REGS i,o;

struct SREGS s;

unsigned char far *file="c:\\command.com";

clrscr();

i.h.al=0;

i.h.ah=0x43;

i.x.dx=FP_OFF(file);

s.ds=FP_SEG(file);

int86x(33,&i,&o,&s);

printf("%u\n\n",o.x.cx);

x=o.x.cx;
tobinary(x);
getch();

}
tobinary(unsigned int n)

{

char *arr[]={



"Read Only",

"Hidden File",

"System File",

"Volume label entry",

"Sub-directory entry",

"Archive bit",

"Unused",

"Unused"


};

int i,k,andmask;

for(i=7; i>=0; i--)

{

andmask = 1 << i;



k = n & andmask;

k == 0 ? printf("0"):printf("1");

printf("\t%s\n",arr[i]);

}

printf("\n");



}
Appendix A



Download 0.91 Mb.

Share with your friends:
1   ...   5   6   7   8   9   10   11   12   13




The database is protected by copyright ©ininet.org 2024
send message

    Main page