# Thread: number representation

1. ## number representation

Hola! im new to C programming and taking some CS classes at the university. My instructor asked us to write a C program to figure out what num notation is used by your machine... 1's, 2's complement or signed magnitude and I dont even know how to begin. Im not loking for code just ideas on how to do this.

gracias

2. First, review your understanding of one's complement, two's complement, and sign and magnitude. Consider say, -1. Assuming an n-bit integer, what is the representation of -1 in each of these systems? Then, check the interesting bits of -1 to see which it corresponds to.

3. Originally Posted by laserlight
First, review your understanding of one's complement, two's complement, and sign and magnitude. Consider say, -1. Assuming an n-bit integer, what is the representation of -1 in each of these systems? Then, check the interesting bits of -1 to see which it corresponds to.
Hola! i know all 3 but dont know how to do it programaticaly. So por example if 4 bits then -1 will be 1111 in 2's complement and 1110 in 1's complement and 1001 in signed magnitude... correcto?

4. Yes. What I have in mind with this suggestion is the use of bitwise operators.

5. Originally Posted by laserlight
Yes. What I have in mind with this suggestion is the use of bitwise operators.
Do you mean << and >> operators... so how to do it with them.

6. Originally Posted by fsanchez
Do you mean << and >> operators... so how to do it with them.
Yes, and also bitwise and. A tutorial on the topic should be enough to get you going, so just search the Web

7. Originally Posted by laserlight
Yes, and also bitwise and. A tutorial on the topic should be enough to get you going, so just search the Web
Muy bien i was able to come up with a program to find no. representation of machine but when i run it on several machines in my lab they all give ans of 2's complement. Now i suppose to explain why and here is code based on your sugestion.
Code:
#define NUMERO 0xF
int main() {
switch (-1&0xF) {
case NUMERO: printf("2's complement\n"); break;
case NUMERO-1: printf("1's complement\n"); break;
default: printf("signed magnitude\n"); break;
}
}

Popular pages Recent additions