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
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 ??
 

btminzon

Full Member level 2
Joined
Jun 12, 2006
Messages
122
Helped
9
Reputation
18
Reaction score
1
Trophy points
1,298
Location
Brazil
Activity points
2,144
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
 

shiv_emf

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
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
 

btminzon

Full Member level 2
Joined
Jun 12, 2006
Messages
122
Helped
9
Reputation
18
Reaction score
1
Trophy points
1,298
Location
Brazil
Activity points
2,144
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

shiv_emf

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
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
 

btminzon

Full Member level 2
Joined
Jun 12, 2006
Messages
122
Helped
9
Reputation
18
Reaction score
1
Trophy points
1,298
Location
Brazil
Activity points
2,144
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?
 

newelltech

Full Member level 4
Joined
Aug 23, 2006
Messages
216
Helped
34
Reputation
68
Reaction score
2
Trophy points
1,298
Activity points
2,929
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

shiv_emf

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
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 ..
 

infosrig

Newbie level 6
Joined
Sep 8, 2009
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,326
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
 

amraldo

Advanced Member level 4
Joined
Aug 29, 2004
Messages
1,185
Helped
145
Reputation
290
Reaction score
40
Trophy points
1,328
Location
Egypt
Activity points
5,880
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

Top