ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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)
* sm2tc - Convert from sign-magnitude 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)
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. ! ~ & ^ | + << >>
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.
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.
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.
aioss: Until you sent me an e-mail 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.
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.
Originally posted by aioss 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
Well, all binary numerals end in one of those 16 combinations (you left out 0001), since there are only 16 possible outcomes for 4 bits.
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".