Not Yet Answered # c syntax help - unsigned int

jephthah 1,888 jephthah 1,888 Discussion Starter dumbncool jephthah 1,888 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

1

n is XOR'ed with an unsigned -1, the resulting value is stored in 'n'

-1 is treated as an unsigned integer, meaning just that it's a variable with all bits are ones. a 32-bit int, will be 0xFFFFFFFF

basically this is like a microcontroller register operation that "flips" all the bits in a register.

i.e., the value 0xA5A5A5A5 when XORed with 0xFFFFFFFF becomes 0x5A5A5A5A

0

the more i look at it though, the more i'm wondering if it's not an incorrectly rendered attempt to make a negative number using 2's complement arithmetic.

because if it were written `( n ^= (unsigned) -1 ) - 1`

(note parentheses) this would be the right hand side (RHS) of an equation that takes the value 'n', inverts it by XORing with 0xFFFFFFFF, then subtracts one ... which gives you the 2's complement binary representation of the original value 'n' made negative. (of course, you'd still have to assign that RHS result to a variable on the LHS for it to be a valid assignment)

so for your example, you have n= 10 which is 0x0000000A

XOR that with 0xFFFFFFFF

gives the result 0xFFFFFFF5

which can either be seen as decimal 4294967285 (unsigned) or -11 (signed). Note that in traditional microprocessor arithmetic, you use registers that dont specify signed or unsigned, it's up to the programmer to interpret it and use it correctly

finally, remember that 2's complement requires you first invert the bits, then subtract one.

so 0xFFFFFFF5 minus 0x00000001 = 0xFFFFFFF4

which is the 2's complement value for -10

.

*Edited 6 Years Ago by jephthah*: n/a

0

Nice explanation. But the writer of the original article where I found this code, just wanted to flip the bits.

Here is that code: It counts the number of set bits in a number.

```
int bitcount (unsigned int n) {
int count = 8 * sizeof(int) ;
n ^= (unsigned int) - 1 ;
while (n) {
count-- ;
n &= (n - 1) ;
}
return count ;
}
```

0

okay then, my first post was correct. that is indeed what he's doing: flipping the bits.

notice the `(unsigned int) - 1`

.... the value of this is an "unsigned -1", aka 0xFFFFFFFF, but it's confusing because he has a space between the negative sign and the one. ie, `"- 1"`

instead of `"-1"`

.

this is not technically incorrect because the compiler ignores the whitespace in this case, but it makes for a confusing read because it makes you think of a subtraction operation -- which it is not. i believe that whitespace is an unintended typo that happened to not get noticed because it didn't affect the program execution.

its why i thought maybe it was a flawed attempt of 2's complement negation, and probably why it was confusing to you also.

.

*Edited 6 Years Ago by jephthah*: n/a

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...