SunQuest
           Dev Shed Lounge
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDev Shed Lounge

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:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old August 1st, 2002, 10:09 PM
wordracr wordracr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 601 wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 9 m 25 sec
Reputation Power: 10
explanation of negative numbers in binary

I've noticed that when a negative number is represented in binary, all bits to the left that would have been zero should be one. I see that... But what's the difference between
how -1 and 255 are represented? "1111 1111"

The windows calculator will covert -1 to that, but when I go back to decimal, it makes it 255..

Reply With Quote
  #2  
Old August 1st, 2002, 10:37 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
It's because the -ve number (-1) is being represented in binary using two's complement. It's the most common way that computers use to represent -ve numbers these days. See http://www.duke.edu/~twf/cps104/twoscomp.html for more information as to the advantages of using this technique.

You can elect to treat a number as signed or unsigned in some languages (such as C, C++, Pascal, Delphi etc.). Basically what this means is that you tell the compiler to either assume that all 8 bits are being used for a positive number (i.e. unsigned), or 7 bits are used for the number and 1 bit for the sign (i.e. signed).

If the number is treated as unsigned, then the range is 0 to 255 (for one byte). If you treat it as signed, then the range is -128 to 127. Either way, you have a range of 256 distinct values. It's just a matter of which range of values you choose to use.

Reply With Quote
  #3  
Old August 2nd, 2002, 04:24 PM
Ctb's Avatar
Ctb Ctb is offline
An Ominous Coward
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2002
Posts: 4,425 Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level)Ctb User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 10 h
Reputation Power: 0
I think this is a very common question because a lot of different books and teachers don't explain the difference between unsigned and signed integers in a program. Instead, all they do is say "OK, flip everything to make it two's complement". This is true, but what they aren't telling you is that the one bit your flipping doesn't get counted in the value as anything more than (+|-). I posed such a question to a goofball C (actually, Hypecard) programming teacher in High School and he couldn't tell me the difference . I also have a Sams C in 24 hours book that doesn't explain that one bit gets reserved for unsigned / signed integers. What a pain.

Reply With Quote
  #4  
Old August 26th, 2002, 07:18 PM
Xdeth's Avatar
Xdeth Xdeth is offline
Defensor fidei
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Raleigh, NC
Posts: 24 Xdeth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Actually, if you only flip the bits of a binary number in two's complement ns you will not get the correct negative or positive number back. You must flip the bits then add 1 to the number applying the carry rule through the most sig bit, or wherever the carry stops. Just flipping the bits changes ones complement sign value. Of course this is all really easy to figure out once you know whats going on. Floating point numbers however are a different story. Alot of folks with degrees in comp-sci can't correctly explain how floating point numbers work in today's popular hardware. It's freakin magic

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherDev Shed Lounge > explanation of negative numbers in binary


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 | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway