TIFF Font Hack InstallerRandom Homebrew: TIFF Font Hack Installer
Allows you to change the system fonts on your 2.0-2.80 PSP
Friends: Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita

[Tutorial] Introduction to programming using C (V)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device

Re: [Tutorial] Introduction to programming using C (V)

Postby N6v7d8 » Tue Jul 10, 2012 3:03 am

Good Morning :)

I have another burning question :o

'a' current value is 10
'b' current value is 1
'c' current value is 1024

but after this line
___________________________________________________
// Binary NOT
c = ~a;
printf("c = ~a\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = ~a\ta = %08X, b = %08X, c = %08X\n", a, b, c);
___________________________________________________

the c became -11 o,o

I understand that 'c' get the value from 'a' which is 10 << is this correct?

I don't get why it add 1? and why it became -11? :?


Thank you :)
User avatar
N6v7d8
 
Posts: 31
Joined: Fri May 25, 2012 6:01 am

Re: [Tutorial] Introduction to programming using C (V)

Postby asgard20032 » Tue Jul 10, 2012 3:55 am

Because the negation (~) reverse all bit: 0 became 1, and 1 become 0. But on signed integer (and other signed variable), the first bit, the most left bit, is a little special. Its used for the sign.

For example, if we take a 4 bit variable, 0 is represented as 0000 in memory. 1 Is represented as 0001 for positive, and as 1000 for negative. So the way to convert from negative to positive, and positive to negative is this:

Positive -> Negative value
Reverse all bit, and add 1.

Negative-> Positive
Reverse all bit, and subtract 1.

Since unsigned type, have 1 more bit used for the number representation than signed, they can hold up bigger number.

So when you read a signed number, if the sign bit is set to 1, just subtract 1, and read it as normally, and add a - sign.

So for your -11, you need to add 1 : -11 + 1 = 10

Also, since 0000 is used for 0, it mean that positive value has 1 possibility less than negative value. Its why signed char have this range value: -128 to 127.
-128 because we can include 11111111 but since 00000000 is used for the 0, positive can't get up to 128.

Most of the time, we use Two complement binary system. Read this: http://en.wikipedia.org/wiki/Two%27s_complement

But it may happen that a language or system(like calculator, some processor's native code) use a One complement system. Read this: http://en.wikipedia.org/wiki/One%27s_complement
Image
asgard20032
 
Posts: 259
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: [Tutorial] Introduction to programming using C (V)

Postby N6v7d8 » Wed Jul 11, 2012 2:24 am

asgard20032 wrote:Because the negation (~) reverse all bit: 0 became 1, and 1 become 0. But on signed integer (and other signed variable), the first bit, the most left bit, is a little special. Its used for the sign.

For example, if we take a 4 bit variable, 0 is represented as 0000 in memory. 1 Is represented as 0001 for positive, and as 1000 for negative. So the way to convert from negative to positive, and positive to negative is this:

Positive -> Negative value
Reverse all bit, and add 1.

Negative-> Positive
Reverse all bit, and subtract 1.

Since unsigned type, have 1 more bit used for the number representation than signed, they can hold up bigger number.

So when you read a signed number, if the sign bit is set to 1, just subtract 1, and read it as normally, and add a - sign.

So for your -11, you need to add 1 : -11 + 1 = 10

Also, since 0000 is used for 0, it mean that positive value has 1 possibility less than negative value. Its why signed char have this range value: -128 to 127.
-128 because we can include 11111111 but since 00000000 is used for the 0, positive can't get up to 128.

Most of the time, we use Two complement binary system. Read this: http://en.wikipedia.org/wiki/Two%27s_complement

But it may happen that a language or system(like calculator, some processor's native code) use a One complement system. Read this: http://en.wikipedia.org/wiki/One%27s_complement


so it will be like -11 to 10? xD

I have another question :(

// Binary AND
c = a & b;
printf("c = a & b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a & b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

// Binary OR
c = a | b;
printf("c = a | b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a | b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

// Binary XOR
c = a ^ b;
printf("c = a ^ b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a ^ b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

return 0;
}

here c = a & b; 'c' become 0 o,o

Is it because int c? xD and not unsign int c? o,o

and here

c = a | b; 'c' become 11 o,o

is it because a and b value add?

and last here, the 'c' current value is 11 but since c were use again.. it will change right? xD

c = a ^ b; 'c' become 11 again o,o

is it because a and b just add again? o,o and the previous 11 value were disregard? xD

Please tell me if this is right or wrong

thank you thank you :)
User avatar
N6v7d8
 
Posts: 31
Joined: Fri May 25, 2012 6:01 am

Re: [Tutorial] Introduction to programming using C (V)

Postby asgard20032 » Wed Jul 11, 2012 3:19 am

-11 + 1 = -10, just forgot to put the negation symbol

I don't know what value you used for your a, b, c variable.

I suggest you to read how bitwise operator work, what happen binary.
Image
asgard20032
 
Posts: 259
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: [Tutorial] Introduction to programming using C (V)

Postby N6v7d8 » Wed Jul 11, 2012 3:35 am

asgard20032 wrote:-11 + 1 = -10, just forgot to put the negation symbol

I don't know what value you used for your a, b, c variable.

I suggest you to read how bitwise operator work, what happen binary.


thanks for the reply

I didn't change the value of 'a' 'b' 'c' xD that was from the front page xD

thank you xD
User avatar
N6v7d8
 
Posts: 31
Joined: Fri May 25, 2012 6:01 am

Re: [Tutorial] Introduction to programming using C (V)

Postby m0skit0 » Wed Jul 11, 2012 8:59 am

@N6v7d8: you cannot keep asking questions without reading the links we give you. To learn you have to read and understand. Please check the various Wikipedia links we put for you. Read them and then ask ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4787
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (V)

Postby N6v7d8 » Wed Jul 11, 2012 9:47 am

m0skit0 wrote:@N6v7d8: you cannot keep asking questions without reading the links we give you. To learn you have to read and understand. Please check the various Wikipedia links we put for you. Read them and then ask ;)


I'm sorry :oops:

and

Thank you :?
User avatar
N6v7d8
 
Posts: 31
Joined: Fri May 25, 2012 6:01 am

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest