Computer system--Data presentation test

Source: Internet
Author: User

This time for a computer system experiment, is to use some basic operators to implement function functions.

PS to do these questions reminds me of the first semester just learned the second system when the ROC brother dominated the pain.

1.

/*   * Bitxor-use only ~ and & to implement XOR  *   Example: Bitxor (4, 5) = 1 *   allowable operator: ~ & *   Maximum number of operators: *   
   
    */
   

Problem-Solving ideas: Simple xor, a⊕b = (¬a∧b) ∨ (a∧¬b) but requires the use of &

int bitxor (int x,int  y) {    return (~ (~x&y) &~ (x&~y)) ;     // ( (~x&y) | ( X&~y));}

2.

/*   * tmin-Returns the smallest twos complement  *   allowed operators:! ~ & ^ | + << >> *   maximum number of operators: 4 *   * /

Problem solving idea: The minimum value is 0x8000 0000, we can move 1 left 31 bits to get the minimum value.

int tmin (void) {    return1<<;}

3.

/* * Istmax-if X is the largest twos complement, returns 1; otherwise, 0 *   allowed Operator:! ~ & ^ | + *   Maximum number of operators: *   /

Solution thinking: The largest binary complement is 0x7fffffff, in order to determine whether the input is this number, we only need to be with the smallest twos complement or to determine whether it is 0.

int istmax (int  x) {    return ! ( x^~ (1<<));}

4.

/*    * Negate-return-X  *   Example: negate (1) = -1. *   allowed Operators:! ~ & ^ | + << >> *   maximum number of operators: 5 *   **

The idea of solving problems: positive number is negative plus one.

int negate (int  x) {    return (~x+1);}

5.

/*   * alloddbits-Returns 1 if all odd digits are 1; 0 *   Example: alloddbits (0xFFFFFFFD) = 0, alloddbits (0xAAAAAAAA) = 1 *   allowable operation Character:! ~ & ^ | + << >> *   maximum number of operators: *   */

Solution:: Only the number of odd digits is 1, and the & operation with 0x5555 5555 will get 0. 5-> (0101). Therefore, we need to get 0x5555 5555, 0x55 (01010101) to the left 8, 16, 24 to get 3 numbers, and then add the three numbers can be 0x5555 5555.

int alloddbits (int  x) {    return ! ( ~ (x| (+ <<8) + ($ << +) +(24 ))));}

6.

/*   * Isasciidigit-if x is 0~9 in ASCII code, return 1; 0 *   Example: isasciidigit (0x35) = 1. *            isasciidigit (0x3a) = 0. *            isasciidigit (0x05) = 0. *   allowed Operators:! ~ & ^ | + << >> *   maximum number of operators: *   

How to solve the problem: if X is a number, then x is between ' 0 ' ~ ' 9 '. can be calculated using x-48>=0 and x-58<0 (X+~48+1>=0 and x+~58+1<0).

int isasciidigit (int  x) {    return ! ( (x+~+1) >>)&!! ((x+~+1) >>);}

Computer system--Data presentation test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.