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...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int y){ x = (x*y) mod 3; y = y – x; } main(){ i = 0; a[0] = 1; a[1] = 2; a[2] = 0; f(i, a[i]); print(“%d %d %d %d\n”, i, a[0], a[1], a[2]); f(a[i], a[i]); print(“%d %d %d\n”, a[0], a[1], a[2]); }
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 ";   ...
int main() { while (1) { float d; scanf("%f", &d); float x; for (x = d;...
int main() { while (1) { float d; scanf("%f", &d); float x; for (x = d; x <= d + 1000.0; x = x + 1000.0) { } printf("loop exited with x = %.14g\n", x); } return 0; } If you run the program, you will see. What number should I use as an input to make this an infinite loop?
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
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...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Here is a bit of Java code: Thing t = new Thing(); t.method( (int i)-> i...
Here is a bit of Java code: Thing t = new Thing(); t.method( (int i)-> i + 1 ); Here is a skeleton for class Thing: class Thing { public interface Mystery { ????? } public void method( Mystery param ) { // body of method omitted } } Which text below would work where ????? is given above: a) void method( int i ); b) int method( int i ); c) abstract int method( int i ); d) abstract...
Data Structures using C++ Consider the definition the following function template: template <class Type> Type func(Type...
Data Structures using C++ Consider the definition the following function template: template <class Type> Type func(Type list[], int size) {        Type x = list[0];        Type y = list[size - 1];        for (int j = 1; j < size / 2; j++)        {               if (x < list[j])                      x = list[j];               if (y > list[size - 1 - j])                      y = list[size - 1 - j];        }        return x + y; }...
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()...