ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

Write a function with the following prototype:
/* Determine whether arguments can be subtracted without overflow */
int tsub_ok(int x, int y);
This function should return 1 if the computation x - y does not overflow

(I am only allowed to use bit-level and logic operation, shifts, and c constants; the code should be straight line with no conditionals, loops, division, modulus, multiplication, or relative comparison operators. Assume 2's complement for signed binaries with w-bit word size)
I want to check for positive overflow and negative overflow respectively and do a bitwise-or. But I don't know how to do each respectively. Any hints?

Do you know the definition of that mentioned (positive/negative) overflow? Do you know how to calculate it anyway?
Do you have already any code written?

While we are willing to help you with homework problems, posting the problem and asking others to work it for you is not very helpful to your understanding and is not an acceptable use of the forum.

I agree that you do not appear to understand the question itself, so the best place to start is to ask your instructor to clarify the meaning of those terms, and review your course material which should have covered these by now.

As hinted by rtmistler, you will need to know the largest and smallest numbers that can be represented by the respective types, then you need to write a simple math expression which will tell you wherher adding, or subtracting some number to or from the current value will overflow or underflow that variable (without actually overflowing or underflowing).

Review your material and ask your instructor if possible, and try to work out your own solution, then ask here if you get stuck.

I don't really have anything so far except for <code shown matching this description>

An additional point here is that using web search terms related to the question do yield results where the problem has been discussed with proposed solutions several times over. Top hits do not appear on this site, but do appear on at least one other technical site where code solutions are discussed.

I do hope that if anyone searches for and finds a full code solutions, that they take the time to test and verify it, as well as learn from it.

The edit says this was here on the 16th, prior to my earlier post, but I certainly missed the minute details:

Quote:

Originally Posted by Portal

(I am only allowed to use bit-level and logic operation, shifts, and c constants; the code should be straight line with no conditionals, loops, division, modulus, multiplication, or relative comparison operators. Assume 2's complement for signed binaries with w-bit word size)

I do feel that one can start here with a classical, fully equipped solution which may use libraries, and some variety of techniques which do not meet the criteria here, but then tune their solution.

You can also construct the code so as to not use any #include files whatsoever, and therefore no libraries. With the exception of console or file I/O, but you can #if 0/1 those parts, use a symbolic debugger, verify with static test data, attain the solution exactly as needed, and then re-introduce the I/O libraries so that you can use a console to input and output, or files, or a combination of both.

As far as overall task flow?

I will be the first to admit that in earlier career or training, I had not always been a good student, or good at addressing tasks.

I would put forth and complete an effort, at the point where my mind had assimilated a path towards a solution.

One can joke about discovering a path towards a solution while doing dozens or entirely non-related, daily life, activities. This is extremely common, at least it is for engineers.

Meanwhile, the issue becomes far worse when the complexity and difficulty of the problem are both very high. One cannot just always just mull over a problem which involves an entire system in their mind. They certainly can, however the level of organization which they employ must be very high.

Therefore it is good advice to find a way to make progress that fits for yourself, or try to find a way to adjust your self to be capable of addressing a problem, and learn how to grow to be capable of contending with design issues of greater complexity.

And I already have described a possible direction for one possible task flow.

At this point, it really comes down to how much a person, and in this case Portal, wishes to learn from their assignments.

I'd say you should perform binary substraction then compare the borrow-bit and the sign-bit: they are equals, then there is no overflow.

Edit: Oh well, I cheated: found the solution with google. It is a bit anticlimatic though: you have to examine the sign of x, y, and x-y. (This has to be performed with a bit test, using a predefined constant from limits.h)

There are definitions for MAX and MINIMUM for most things in a header file somewhere, if you can find it
Or you could maybe use the size (in bytes) of the things in question to determine the answer?

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.