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.

Keil C51 right shift for 16 bit int

Status
Not open for further replies.

hithesh123

Full Member level 6
Joined
Nov 21, 2009
Messages
324
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
lax
Activity points
3,548
When I try to right shift 16bit data, the MSbit gets loaded with 1.

mydata=0x8000;
mydata>>=1;

Now, mydata becomes 0xC000;

Does the 16bit shift work differently in keil C51.
My Mcu is C8051F120.
 

PHP:
0x8000 >> 1 = 0x4000
0x4000 >> 1 = 0x2000
0x2000 >> 1 = 0x1000
0x1000 >> 1 = 0x0800
0x0800 >> 1 = 0x0400
0x0400 >> 1 = 0x0200
0x0200 >> 1 = 0x0100
0x0100 >> 1 = 0x0080
0x0080 >> 1 = 0x0040
0x0040 >> 1 = 0x0020
0x0020 >> 1 = 0x0010
0x0010 >> 1 = 0x0008
0x0008 >> 1 = 0x0004
0x0004 >> 1 = 0x0002
0x0002 >> 1 = 0x0001
 

PHP:
0x8000 >> 1 = 0x4000
0x4000 >> 1 = 0x2000
0x2000 >> 1 = 0x1000
0x1000 >> 1 = 0x0800
0x0800 >> 1 = 0x0400
0x0400 >> 1 = 0x0200
0x0200 >> 1 = 0x0100
0x0100 >> 1 = 0x0080
0x0080 >> 1 = 0x0040
0x0040 >> 1 = 0x0020
0x0020 >> 1 = 0x0010
0x0010 >> 1 = 0x0008
0x0008 >> 1 = 0x0004
0x0004 >> 1 = 0x0002
0x0002 >> 1 = 0x0001

Jayanth,

My point was - when you expect 0x8000 to become 0x4000, in keil C51 I get 0xc000.
 

i think you are doing again ORing with last data that is 0x8000 >> 1 = 0x4000
and after ORing
0x8000 | 0x4000 = 0xc000.
 

mydata=0x8000;
mydata>>=1;

Now, mydata becomes 0xC000;
That's the expectable result if mydata is signed int (arithmetic shift).
You may want to use an unsigned int data type.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top