• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/40

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

40 Cards in this Set

  • Front
  • Back

C for loop

int factorial1(int n)

{


int res = 1;


int i = 1;


for(i=1;i<=n;i++)


res=res*i;


return res;


}

C while loop

int factorial2(int n)

{


int res = 1;


int i = 1;


while(i<=n)


{


res = res*i;


i++;


}


return res;


}

C do loop

int factorial3(int n)

{


int res = 1;


int i = 1;


do


{


res = res*i;


i++;


}


while(i<=n);


return res;


}

C Recursion

int factorial4(int n)

{


if (n >= 1)


return n*factorial4(n-1);


else


return 1;


}

CHAR_BIT

8

SCHAR_MIN

-128

SCHAR_MAX

+127

UCHAR_MAX

255

CHAR_MIN

-128

CHAR_MAX

+127

MB_LEN_MAX

16

SHRT_MIN

-32768

SHRT_MAX

+32767

USHRT_MAX

65535

INT_MIN

-2147483648

INT_MAX

+2147483647

UINT_MAX

4294967295

LONG_MIN

-9223372036854775808

LONG_MAX

+9223372036854775807

ULONG_MAX

18446744073709551615

How to print the value of limits.h

int main() {


printf("The number of bits in a byte %d\n", CHAR_BIT);


}


2.59

Write a C expression that will yield a word consisting of the least significant byte of x, and the remaining bytes of y. For operands x = 0x89ABCDEF and y = 0x76543210, this would give 0x765432EF
int prob259(int x, int y)

{


return (x & 0xFF) | (y & ~ 0xFF);


}

2.60


Suppose we number the bytes in a w-bit word from 0 (least significant) to w/8 − 1(most significant). Write code for the following C function, which will return anunsigned value in which byte i of argument x has been replaced by byte b:


unsigned replace_byte (unsigned x, int i, unsigned char b);




Here are some examples showing how the function should work:replace_byte(0x12345678, 2, 0xAB) --> 0x12AB5678replace_byte(0x12345678, 0, 0xAB) --> 0x123456AB

unsigned replace_byte(unsigned x, int i, unsigned char b)

{


return ( x & ~(0xFF << (i << 3))) | (b << (i << 3));


}

2.61

Write C expressions that evaluate to 1 when the following conditions are true, andto 0 when they are false. Assume x is of type int.


A. Any bit of x equals 1.


B. Any bit of x equals 0.


C. Any bit in the least significant byte of x equals 1.


D. Any bit in the most significant byte of x equals 0.


Your code should follow the bit-level integer coding rules (page 120), with theadditional restriction that you may not use equality (==) or inequality (!=) tests.

int prob261A(int x)

{


return !!x ;


}



int prob261B(int x)


{


return !x;


}



int prob261C(int x)


{


return !!(x & 0xFF);


}

2.68

Write code for a function with the following prototype:


/** Mask with least signficant n bits set to 1


* Examples: n = 6 --> 0x2F, n = 17 --> 0x1FFFF


* Assume 1 <= n <= w*/




int lower_one_mask(int n);




Your function should follow the bit-level integer coding rules (page 120). Becareful of the case n = w.

int lower_one_mask(int n){

return (2<<(n-1))-1;


}

* bitAnd - x&y using only ~ and |

* Example: bitAnd(6, 5) = 4


* Legal ops: ~ |


* Max ops: 8


* Rating: 1

int bitAnd(int x, int y) {

return ~(~x | ~y);


}

bitNor - ~(x|y) using only ~ and &


* Example: bitNor(0x6, 0x5) = 0xFFFFFFF8


* Legal ops: ~ &


* Max ops: 8


* Rating: 1

int bitNor ( int x, int y) {


return (~x & ~y);


}

* upperBits - pads n upper bits with 1's

* You may assume 0 <= n <= 32


* Example: upperBits(4) = 0xF0000000


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 10


* Rating: 1

int upperBits ( int n) {


int mask = (!!n) << 31 >> 31;


return mask & (( 1 << 31) >> (n +~0));

* allEvenBits - return 1 if all even-numbered bits in word set to 1

* Examples allEvenBits(0xFFFFFFFE) = 0, allEvenBits(0x55555555) = 1


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 12


* Rating: 2

int allEvenBits (int x) {


int mask = 0x55;


mask |= mask <<8;


mask |= mask <<16;


int y = x | mask;


return !(x^y);

* byteSwap - swaps the nth byte and the mth byte


* Examples: byteSwap(0x12345678, 1, 3) = 0x56341278


* byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD


* You may assume that 0 <= n <= 3, 0 <= m <= 3


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 25


* Rating: 2

byteSwap(int x, int n, int m) {


int mask, xx, nn,mm, nx, mx;


nn = n << 3;


mm = m << 3;


nx = x >> nn;


mx = x >> mm;


xx = ((mx&0xFF) << nn)|((nx&0xFF)< <mm);


mask = (0xFF << nn) | (0xFF << mm);


return (x & ~mask) | xx;


}

logicalShift - shift x to the right by n, using a logical shift

* Can assume that 0 <= n <= 31


* Examples: logicalShift(0x87654321,4) = 0x08765432


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 20


* Rating: 3

int logicalShift ( int x, int n) {


int mask ((1<<31)>> n) <<1;


return (x >> n ) & ~ mask;

* bitParity - returns 1 if x contains an odd number of 0's


* Examples: bitParity(5) = 0, bitParity(7) = 1


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 20


* Rating: 4

int bitParity(int x) {


x ^= x >> 16;


x ^= x >> 8;


x ^= x >> 4;


x ^= x >> 2;


x ^= x >> 1;


return x & 1;


}

* tmin - return minimum two's complement integer

* Legal ops: ! ~ & ^ | + << >>


* Max ops: 4


* Rating: 1

int tmin (void) {


return 1 << 31;


}

* isNegative - return 1 if x < 0, return 0 otherwise * Example: isNegative(-1) = 1.

* Legal ops: ! ~ & ^ | + << >>


* Max ops: 6


* Rating: 2

int isNegative (int x) {


return (x >> 31) & 0x1;


}

* sign - return 1 if positive, 0 if zero, and -1 if negative

* Examples: sign(130) = 1


* sign(-23) = -1


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 10


* Rating: 2

int sign (int x) {


return (x>> 31) | !!x;


}

* isEqual - return 1 if x == y, and 0 otherwise


* Examples: isEqual(5,5) = 1, isEqual(4,5) = 0


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 5


* Rating: 2

int isEqual (int x, int y) {


return !(x^y);


}

* divpwr2 - Compute x/(2^n), for 0 <= n <= 30

* Round toward zero


* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2 * Legal ops: ! ~ & ^ | + << >>


* Max ops: 15


* Rating: 2

int divpwer2 (int x, int n) {


int mask = (1 << n) + ~0;


int bias = (x >> 31) & mask;


return (x+bias) >> n;


}

* isLessOrEqual - if x <= y then return 1, else return 0

* Example: isLessOrEqual(4,5) = 1.


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 24


* Rating: 3

int isLessOrEqual (int x, int y) {


int sgnx = (x>>31)&0x1;


int sgny = (y>>31)&0x1;


int d = x + (~y+0x1);


int sgnd = (d>>31)&0x1;


return !(x^y) | (sgnx&!sgny) | (!(sgnx^sgny)&sgnd);


}

* conditional - same as x ? y : z


* Example: conditional(2,4,5) = 4


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 16


* Rating: 3

int conditional ( int x, int y, int z) {


int mask = !x + ~0;


return (y & mask) | (z & ~mask);

* addOK - Determine if can compute x+y without overflow

* Example: addOK(0x80000000,0x80000000) = 0, * addOK(0x80000000,0x70000000) = 1,


* Legal ops: ! ~ & ^ | + << >>


* Max ops: 20


* Rating: 3

int addOk ( int x, int y) {


int xneg = x >>31;


int yneg = y >> 31;


int xpyneg = (y + x )>>31;


return ! ((~xneg & ~yneg & xpyneg) | (xneg & yneg & ~xpyneg));