Random Homebrew: TIFF Font Hack InstallerAllows 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)

Good Morning

I have another burning question

'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

N6v7d8

Posts: 31
Joined: Fri May 25, 2012 6:01 am

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

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
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)

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

N6v7d8

Posts: 31
Joined: Fri May 25, 2012 6:01 am

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

-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.
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)

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.

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

thank you xD

N6v7d8

Posts: 31
Joined: Fri May 25, 2012 6:01 am

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

I wanna lots of mov al,0xb

"just not into this RA stuffz"

m0skit0
Guru

Posts: 4787
Joined: Mon Sep 27, 2010 6:01 pm

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

I'm sorry

and

Thank you

N6v7d8

Posts: 31
Joined: Fri May 25, 2012 6:01 am

Previous