C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 21st, 2005, 06:49 AM
andy099 andy099 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 127 andy099 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 2 m 33 sec
Reputation Power: 9
Bit Shift Problem

I have a bit of a problem with this code.

I am dealing with 32 bit messages and on of the fields within this message is 21 bits long.
It also scaled to provide a range of -180 to + 180; i.e. LSB = 0.000172.
I have for each field its mask for the message; i.e. in this case 0x1FFFFF00. Also the shift value from the right (LSB) = 8 in this case.

Code:
unsigned int currentData;    // This is the 32 bit message
unsigned long currentField;  // The 21 bit field 

Now if I have to set the field to 34.56 (float fEngValue = 34.56) and using the scaling factor (float m_fScalingFactor = 0.000172) as follows:-

Code:
currentField = (unsigned int)(fEngValue/m_fScalingFactor);
newData = (unsigned int)(currentField);

// Clear current field and leave the other fields intact
unsigned int maskData = ~mask;
unsigned int otherData = currentData & maskData;
unsigned int bitField = (newData << shift);
// Or the new data with the old fields together and write to memory
*(unsigned int *) fieldPtr = bitField | otherData;
This works fine for +ve numbers, but NOT for -ve numbers.

Again I have a mental block for negative numbers.

Any suggestions please.

Its Visual Studio .Net; C++ Windows platform.
Andy.

Reply With Quote
  #2  
Old October 21st, 2005, 07:20 AM
pflloyd pflloyd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 150 pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level)pflloyd User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
you probably having problems with negative numbers because of the way they are stored. computers use 2's complement and signed notation. with 2's complement invert all the bits and then add 1. signed notation carries around an extra bit that determines the sign.

a binary -4 in signed notation and 2's complement

1 0100 - first bit is sign bit

1100 - 2's complement (0100 => 1011 + 1 = 1100)

hope this helps
Comments on this post
mike65535 agrees!

Reply With Quote
  #3  
Old October 21st, 2005, 07:52 AM
andy099 andy099 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 127 andy099 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 2 m 33 sec
Reputation Power: 9
OK Thanks

Yes, so if I now set the -ve value to +ve and set a flag, I can then set the sign bit for the -ve case.

Many thanks, having a mind block today.

Andy.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Bit Shift Problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap