Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

LEFT shift operator for numbers

Status
Not open for further replies.

shiv_emf

Advanced Member level 2
Advanced Member level 2
Joined
Aug 31, 2005
Messages
605
Helped
22
Reputation
44
Reaction score
6
Trophy points
1,298
Activity points
4,106
Hii guys... i am confused with left shift operator ...

suppose i left shifting a NUMBER ..will the sign of number changes??
for example ...
suppose for integer type variable ... size of integer is 4bytes tht is 32 bits

system fallows 2's complement representation ... if v shift smalllest negative number ...what will be the result ??
 

Re: LEFT shift operator

Hi shiv_emf...

In C, if your variable is signed, the signal will be extended, but if thew variable is unsigned, the compiler automatic zero filling. Is this your doubt? i hope i were helpfull
 

LEFT shift operator

hey thanks for ur reply !!

but
int x;

this statement is signed integer ... if i leftshift it tht is
100000000000000000000000000 (1 in msb and 31 "zero")
wht will be the output ??
it is zero or same number as .. signed bit shud nt be changed
 

Re: LEFT shift operator

it´s better you force zero initializing. for exemple

int x = 0;

x = x <<31;

the output is x = 0;

if
x = 1;
x = x << 31;
then

x = 0x80000000;

was i clear? its better to initialize your int. if you use unsigned int, its automatcally zero initialized.
 

    shiv_emf

    Points: 2
    Helpful Answer Positive Rating
LEFT shift operator

another one

in case of Negative number !!.... system stores these values in 2's complement

now if u leftshift variable ..... will Signed BIT changes???

i hope i am clear now
 

Re: LEFT shift operator

Hi shiv_emf, thanks for "Helped". It was a pleasure.

Now, if your variable, signed, is negative and left shift, the signal will be kepted. It´s the same os thats instructions in asm that rotate left keeping the signal. Was i clear?
 

Re: LEFT shift operator

You need to be careful on this one.
Right shifts are implementation dependent. That means that you need to look up how your compiler treats signed right shift with negative values. It could be either a logical shift (you lose the sign bit and it becomes 0) or arithmetic shift ( sign bit is kept).

All Microsoft compilers use arithmetic shifts.

The easiest way to tell is to write some test code and set an int to a negative value, right shift it one and use the debugger to look at the result.
 

    shiv_emf

    Points: 2
    Helpful Answer Positive Rating
LEFT shift operator

nice one ... thanks for help :)

well i am working on REDHAT linux OS ... using GCC

so i hav to chk it out ..
 

Re: LEFT shift operator

shiv_emf said:
another one

in case of Negative number !!.... system stores these values in 2's complement

now if u leftshift variable ..... will Signed BIT changes???

i hope i am clear now


new operator <<< try it
 

Re: LEFT shift operator

Verilog 2001 defined >>> and <<< for signed shifting operations.
--
Amr Ali
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top