Question

Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;...

Evaluating if a value is negative using bitwise operators

int test_dl3(int x) {
    int i;
    for (i = 0; i < 32; i+=2)
       if ((x & (1<          return 0;
           return 1;
}
Legal ops: ! ~ & ^ | + << >>
Max ops: 12

I have a few questions similar to this one, but I'm running into much the same problem for all of them.
The behavior of this code appears to be that its supposed to return a 1 when the input x is negative and a zero otherwise. I have gone through many different ways of looking at it, but they all give about the same result so heres one example.
((x >> 31) & 1)

The issue I come across is that with the value 0x80000000 it evaluates it as -2147483648 (a negative) when it should be evaluated as a zero. Not sure how I should be approaching these problems differently. Thanks in advance

Homework Answers

Answer #1

#include <iostream>
using namespace std;
int test_dl3(int x) {
int n=x>>31;
int mask=n>>31;
return ((n+mask)^mask);
}
int main() {
   cout<<test_dl3(-50)<<endl;
   cout<<test_dl3(0)<<endl;
   cout<<test_dl3(3)<<endl;
   return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
/* logicalShift - shift x to the right by n, using a logical shift can assume...
/* logicalShift - shift x to the right by n, using a logical shift can assume that 0<= n <=31 Examples: logicalShift(0x87654321,4) = 0x08675432 Legal ops: ! ~ & ^ | + << >> Max ops: 20 Rating: 3    */ int logicalShift(int x, int n) { return 0; } How would I code the logicalShift in C? I have this, but this isn't right. I think I'm in the right track though: int logicalShift(int x, int n) { int a...
I'm looking for an in-depth explanation on how each of these bitwise functions work. I think...
I'm looking for an in-depth explanation on how each of these bitwise functions work. I think I understand the underlying concepts of each, but I want to be sure. I'm looking for a piece by piece break down of each function. Function 1 int logicalShift(int x, int n) { x >>= n; return x & ~(~(~0 << n) << (32 + ~n + 1)); Function 2 { int bang(int x) { int neg = ~x + 1; return ((~(x |...
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...
C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you! I have attatched the homework instructions and the two files given. Implementation The main program, called calculator.cpp...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
I'm generating a random number every 10 second and Storing and Updating the number every 10...
I'm generating a random number every 10 second and Storing and Updating the number every 10 second when I generate new number in UpdateInt method. However, In my main method when I called UpdateInt method it generate every 10 second new number but it doesn't update and store in the arraylist. ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number*** import java.util.ArrayList; import...
The decimal values of the Roman numerals are: M D C L X V I 1000...
The decimal values of the Roman numerals are: M D C L X V I 1000 500 100 50 10 5 1 Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40. Assignment: Begin by creating a new project in the IDE of your choice. Your project should contain the following: Write a class romanType. An object of romanType should have the following...
I cannot for the life of me get this program to run properly. I don't know...
I cannot for the life of me get this program to run properly. I don't know what I'm doing wrong. Could the format of my text files be the issue? Edit: The program works this way, you choose a year and a gender and then enter a name. When you press the button its should give you the ranking or popularity of the name. There are 5 files that I have to search through, named 2006.txt to 2010.txt. First the...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...