can anyone help me with following bit-wise manipulation.thanks!

/* 
 * copyLSB - set all bits of result to least significant bit of x
 *   Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int copyLSB(int x) {


}
 *
 * logicalShift - shift x to the right by n, using a logical shift
 *   Can assume that 1 <= n <= 31
 *   Examples: logicalShift(0x87654321,4) = 0x08765432
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 16
 *   Rating: 3 
 */
int logicalShift(int x, int n) {



  return 2;

}
/*
 * bitCount - returns count of number of 1's in word
 *   Examples: bitCount(5) = 2, bitCount(7) = 3
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 40
 *   Rating: 4
 */
int bitCount(int x) {

    return(x>>1); 


}
/* 
 * bang - Compute !x without using !
 *   Examples: bang(3) = 0, bang(0) = 1
 *   Legal ops: ~ & ^ | + << >>
 *   Max ops: 12
 *   Rating: 4 
 */
int bang(int x) {






  return 2;

}
/* 
 * leastBitPos - return a mask that marks the position of the
 *               least significant 1 bit. If x == 0, return 0
 *   Example: leastBitPos(96) = 0x20
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 4 
 */
int leastBitPos(int x) {




  return 2;

}
/* 
 * TMax - return maximum two's complement integer 
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 4
 *   Rating: 1
 */
int tmax(void) {



  return 2;

}
/* 
 * isNonNegative - return 1 if x >= 0, return 0 otherwise 
 *   Example: isNonNegative(-1) = 0.  isNonNegative(0) = 1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 6
 *   Rating: 3
 */
int isNonNegative(int x) {



  return 2;

}
/* 
 * isGreater - if x > y  then return 1, else return 0 
 *   Example: isGreater(4,5) = 0, isGreater(5,4) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 24
 *   Rating: 3
 */
int isGreater(int x, int y) {





  return 2;

}
/* 
 * 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 divpwr2(int x, int n) {
    return 2;

}
/* 
 * abs - absolute value of x (except returns TMin for TMin)
 *   Example: abs(-1) = 1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 10
 *   Rating: 4
 */
int abs(int x) {




  return 2;

}
/* 
 * 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) {
  return 2;

}

Dani AI

Generated

This thread looks exactly like the CS:APP "Data Lab" bit‑manipulation exercises. posted the skeleton; correctly noted it appears to be an assignment and replied. Below are compact, accurate patterns and small implementations that solve common tasks in that set, plus practical cautions (Tests assume 32‑bit two's‑complement ints).

Core tricks (with code examples):

  • Broadcast a single bit to all bits: shift it into the sign position and arithmetic‑shift back.
  • Logical right shift via arithmetic right shift plus a mask.
  • Count bits with the SWAR (parallel) method to avoid loops.
/* copyLSB: all bits = least significant bit */
int copyLSB(int x) {
    return ((x & 1) << 31) >> 31;
}

/* logicalShift: logical right shift using mask (1 <= n <= 31) */
int logicalShift(int x, int n) {
    int arith = x >> n;
    unsigned mask = (1u << (32 - n)) - 1u;
    return arith & (int)mask;
}

/* bitCount: SWAR/popcount in a few arithmetic steps */
int bitCount(int x) {
    unsigned m1 = 0x55555555u;
    unsigned m2 = 0x33333333u;
    unsigned m4 = 0x0f0f0f0fu;
    unsigned u = (unsigned)x;
    u = (u & m1) + ((u >> 1) & m1);
    u = (u & m2) + ((u >> 2) & m2);
    u = (u + (u >> 4)) & m4;
    u = u + (u >> 8);
    u = u + (u >> 16);
    return (int)(u & 0x3fu);
}

Short, practical hints for the other functions (use these one‑liners or their equivalents under restricted ops):

  • bang(x): ((x | (~x + 1)) >> 31) + 1
  • leastBitPos(x): x & (~x + 1)
  • tmax(): ~(1 << 31)
  • isNonNegative(x): !(x >> 31)
  • divpwr2(x,n): (x + ((x >> 31) & ((1 << n) - 1))) >> n (bias for negatives)
  • abs(x): int m = x >> 31; (x ^ m) - m
  • addOK(x,y): detect overflow via sign checks on x, y, and x+y
  • isGreater(x,y): handle sign differences first, otherwise test sign of x - y

Cautions: in portable C, left‑shifting a signed 1 into the sign bit can be undefined; prefer unsigned constants (e.g., 1u << 31) when forming masks. Also test edge cases (0, -1, INT_MIN, shifts of 31) and verify behavior on your compiler if the assignment enforces strict operator limits.

Recommended Answers

All 2 Replies

!Yes

I'm a little confused with exactly what this is. Why is almost everything returning 2? Is this a copy/paste from an assignment? Are you expecting us to do the work for you? This isn't a "specific" question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.