C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-08-2004, 07:22 AM   #1
Registered User
 
Join Date: Nov 2004
Posts: 23
bit patterns of negtive numbers?

hi there:

i am just learning more about data types and thought i will write a short code to see the bit patterns of signed variable, in this case a char.

i know that the first bit of signed data types is called sign bit, so i suspected to see something like:

decimal 1 -> 00000001
decimal -1 -> 10000001

the left most bit being the sign bit.

however, in the code i worte i got:

decimal value -> -1 :: bit pattern -> 11111111
decimal value -> 1 :: bit pattern -> 00000001

and it seems that -128 comes after +127 as:

decimal value -> 127 :: bit pattern -> 01111111
decimal value -> -128 :: bit pattern -> 10000000

my question is that, if this is the correct bit representation of signed char, how does negative numbers are convereted into bit patterns? of course, it could be something wrong in the code i wrote, my code are as below:

main()
{
char string[9];
int i;
char test;
unsigned char MASK;

for(test = -128; test<127; test++)
{
MASK = 1<<7;
for(i=0; i<8; i++)
{
sprintf(string+i, "%d", (test & MASK) ? 1 : 0);
MASK>>=1;
}
string[8] = (char)NULL;
printf("decimal value -> %d :: bit pattern -> %s\n", test, string);
}
}

many thanks

CHUN
chunlee is offline   Reply With Quote
Old 11-08-2004, 07:30 AM   #2
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
Negation is accomplished using something called two's complement. Do a search on it to find out more but basically to convert a value from positive to negative, you negate all the bits (zeros become ones and ones become zeros) and then you add 1. So, if 1 is 00000001 then to negate it we first takes its ones complement which is 11111110 and then add 1 to get the two's complement which is 11111111. 127 is 01111111 so -127 is 10000000 (ones complement) and add 1 to get the final answer of 10000001. -128 is 1 less than -127 so take 10000001 and subtract 1 and you get 10000000.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

Last edited by hk_mp5kpdw; 11-08-2004 at 07:33 AM.
hk_mp5kpdw is offline   Reply With Quote
Old 11-08-2004, 07:39 AM   #3
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,730
Code Tags

The reason for the twos complement is so you can add signed numbers and get the correct result.

1 + -1 = 0

Code:
 00000001
+11111111
----------
100000000
But you only take the least signficant bits (in this case 8) so you get 00000000
Thantos is offline   Reply With Quote
Old 11-08-2004, 08:10 AM   #4
Registered User
 
Join Date: Nov 2004
Posts: 23
thanks, i will look into it in more depth.

cheers

CHUN
chunlee is offline   Reply With Quote
Old 11-08-2004, 08:20 AM   #5
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
Quote:
Originally Posted by chunlee
i am just learning more about data types and thought i will write a short code to see the bit patterns of signed variable, in this case a char.

i know that the first bit of signed data types is called sign bit, so i suspected to see something like:

decimal 1 -> 00000001
decimal -1 -> 10000001
There are three possible representations of signed integral types in C:
  • sign and magnitude
  • two's complement
  • one's complement
A brief description is here. Use Google for more.
__________________
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*

Last edited by Dave_Sinkula; 11-08-2004 at 08:27 AM. Reason: i before e except after c
Dave_Sinkula is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
bit level permutation function zxcv C Programming 2 07-27-2008 01:26 PM
Logical Operations on Bit Patterns mthemapc C++ Programming 7 02-17-2008 03:04 PM
the definition of a mathematical "average" or "mean" DavidP A Brief History of Cprogramming.com 7 12-03-2002 11:15 AM
how to pack 8 x 4 bit binary numbers into a long int? n00bcodezor C Programming 11 11-19-2001 05:46 PM
Array of boolean DMaxJ C++ Programming 11 10-25-2001 11:45 PM


All times are GMT -6. The time now is 12:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22