Two answers to the written question

Source: Internet
Author: User

Yesterday brother to take part in the written test, come back and I discussed a few questions quite interesting, now put up two questions to share.

First question

Char 0xFF;   // 1111 1111printf ("%d", c);  // output in integer form

Some people write directly is 255, in fact, this is not in-depth study, I will often make such mistakes;

Below we carefully discussed, Char is eight-bit signed, so his size range is in -128~127, but the body we say 255 is necessarily wrong, if the transposition of unsigned char, then output 255 is definitely right.

Here we explore the answer is how many, some people may directly converted to negative-127, these people are wood to understand the storage mechanism, negative, including the presence of the form of complement .

So the real answer to this question is-1.

Here are two ways to translate the numbers:

1) Minus one counter (this is reverse thinking, because getting the complement is taking the opposite plus one);

2) Add an inverse

The second way we're going to talk about it.

Reference: http://blog.csdn.net/studyvcmfc/article/details/7606292

There: Source code = "" Source code "complement" complement

This method avoids calling some carry in the calculation, it saves a lot of things, although the previous one is also very simple, but still recommend this.

Note: This method is actually a senior recommend, I began to oppose, and later was persuaded.

The second question :

Description: We know that there is a structure to store student information, but we do not know what information is included in the structure, however, we understand that student information combined with weight contains a phone number field, as follows

struct student{...    .     int Mobile_number;    ....} sstudent;

Please use C + + expressions to calculate the offset of mobile_number in Sstudent.

First of all, we want to understand the offset, the Computer assembly language offset is defined as: the actual address of the storage unit and its segment address of the distance between the segment is called the offset, also known as "valid address or offset."

We can write the code after we know it, but the code is simple.

(unsigned int) (& (sstudent*) 0)->mobile_number);

This line of code can be implemented, let's explain below. Refer to the explanations of other great gods.

Reference: http://blog.csdn.net/niu91/article/details/17881773

1.(struct*) 0 means that the constant 0 is coerced to the address pointed to by the struc*-type pointer, where the value of the 0-bit starting address, if replaced by another value, is changed accordingly. have been measured. To better calculate the offset, taking 0 is a good way

2.& (((struct*) 0)->e) represents the address of the Member E that takes the struct pointer (struc*) 0

3. Cast to the unsigned int type.

Since the above mentioned 0 can be changed, there is another way: (unsigned int) (& (sstudent*)->mobile_number)-(unsigned int) ((sstudent*) 1000); /(1000 is arbitrarily obtained)

Summary: There are a lot of mechanisms for C/+ + to let me find out, yesterday I really know a little more. In fact, the examination questions are not difficult, know a little bit can be a little bit, so to deal with this problem, or to read a good book.

Two answers to the written question

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.