user466534
user466534

Reputation:

signed shifting

How can I implement signed shifting in C++? For example this code

#include<iostream>
using namespace std;

int bit_number( int x){
 int total=0;
 while(x){
  total++;
  x>>=1;
 }

 return total;
}

int main(){
 int x=10;
 //cout<<bit_number(x)<<endl;
 int a=bit_number(x);
 int b=2*a;
 x<<=(b-a);
 x=x>>>(b-a);

 while(x!=0){
  int k=x%2;
  x=x>>1;
  cout<<k<<endl;
 }

 return 0;
}

shows me this error:

Error   1   error C2059: syntax error : '>' c:\users\datuashvili\documents\visual studio 2010\projects\binst\binst\binst.cpp    19  1   Binst

I know that in java there is signed shift, what about C++? How can I do it?

Upvotes: 2

Views: 159

Answers (2)

phimuemue
phimuemue

Reputation: 35983

C++ "knows" shifting for signed integers, however, the result is implementation-defined.

In C++, >> denotes shifting (for unsigned and signed types). So, the compiler interprets your >>> as >> followed by > (or the other way around).

Upvotes: 1

Steven
Steven

Reputation: 3928

You have 3 >>> on line 19 (see your error message)

Upvotes: 1

Related Questions