Binary Puzzle
Do any fo you techie's think you can solve this. I have about 4 hrs into it and still unsolved. No logical operators, i.e. if, else, == , , && , etc. ONLY binary ops as indicated in the snippit.
* bitParity  returns 1 if x contains an odd number of 0's * Examples: bitParity(5) = 0, bitParity(7) = 1 * Legal ops: ! ~ & ^  + << >> * Max ops: 20 * Rating: 4 */ int bitParity(int x) { return ; } One more. * sm2tc  Convert from signmagnitude to two's complement * where the MSB is the sign bit * Example: sm2tc(0x80000005) = 5. * Legal ops: ! ~ & ^  + << >> * Max ops: 15 * Rating: 4 */ int sm2tc(int x) { return ; } example I am using the examples give for a word size 16 (to keep it simple) 5= 0000 0000 0000 0101 this will return 0 because it has an even number of zeros. Where as 7 = 0000 0000 0000 0111 this will return 1 because it has an odd number of zeros. Now keep in mind no logic can be used, only bit leve ops. ! ~ & ^  + << >> 
Quote:
<edit> Oops! I was quite wrong here! I answered to quickly, and stupidly! </edit> 
No, if word size is 16 as I showed in my example "5= 0000 0000 0000 0101" and there is two 1's then that would leave you an even number (14 zeros) of zeros. So the retun value by the function would be a 0 because it does not have an odd number of zeros. Understand? I don't know the answer that is why I am asking for help.

Yes, I understand, see my <edit> that I did at the same time as your post.

Are loops (for / while) allowed?

Sorry, no loops or anything logical. I.E. for, while, if ,else, &&, , !=, etc. ONLY binary level operators (! ~ & ^  + << >>) that is all. really dont need them anyhow, just neet to test for one case, cuz then i could say !num and that would essentially be my if/else statement. Don't confuse !num with !=num where the second is not allowed. != is a logical operation. ! is a binary. I hope someone smarter than me can help.

it assumes 32bit ints, i suppose your gonna tell me that another function is also cheating but i cant see how to keep it under 20 ops without using another function or a loop.
Code:
int a(unsigned int num, int bit) 
LOL, actually u can't call another function but I did forget to mention that you can use a constant of up int constant 255 (0000............0000 1111 1111). I sort of follow your logic, very creative and I think if I used it with a constant I could reduce the number of operations. Thank you very much.

Sorry...
aioss: Until you sent me an email I assumed it was a "puzzle" like you said. Now I realize it actually is homework. I'm feeling stupid a second time in this thread, though a different way this time.
I do like puzzles, but not doing somebodies homework. That's cheating from your part both ways:[list=1][*]to your school/college for getting higher grades by cheating.[*]to (ab)use the willingness to help of people here by calling your homework a "puzzle".[/list=1] kev82: No need for an seperate function. I suppose even assignment operator s like "+=" are not allowed. I solved it using only ( ) & ^ << in one big return statement, (aioss:here's some help to get you started anyway) 
Im sorry if I made you feel that way. I was not trying to abuse the term puzzle, the program was called puzzles. Just as a habbit I called it that. I mention that a level 4 had been solved and all level 1,2, and 3's granting me an A on the hwk. I have an A in the class now. Cheating is not how I maintaing a GPA because when it comes time for an exam, they will know if you cheated or did it on your own. I should have stated for someone to make suggestions rather than solve it. I would prefer this. I think "+=" would be allowed. Once agian I am truly sorry to make you react the way you did, never my intention. But thanks for all you did.

Quote:
Quote:
Quote:
Sorry I reacted a bit harsh on you. (feeling stupid a third time :) ) 
Well...
The solution I found would not validate I guess, because it uses 3 ops per bit. So, for 16 bit int's I would need 48 operators. Code:
/* assuming 4 bits for brevity */ 
Since I know it's homework, I'll just give a hint (for the first problem):
Think about what happens when you XOR two equallength numerals together, in particular what happens depending on whether you have an odd or even number of "1" bits in total. Now think of a good way to divide a long numeral (like 32 bits) into smaller pieces so you don't have to perform 32 steps on it in order to get an answer. btw, I am not too clear on why "+" is allowed... it's not really a binary operation (not bitwise, I mean). If + is allowed, why not , *, / ? 
I had a theory but it is a few days old. I noticed from my truth tables that all unsigned ints will end in one of these 15 combinations 0000, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, and I would only need to test agianst half of them for even or odd, and could shift the first four bits into a variable but this would require logic so once agian im stuck.

Quote:
By the way, all you need to do with the second problem is convert all the rest of the bits into two's complement form if the sign bit is 1. Converting them to two's complement form is easy if "+" is allowed (and only marginally harder if it isn't allowed). All you gotta do is find a way to do that based on the sign bit without using an "if". 
All times are GMT 5. The time now is 02:40 PM. 