| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Bit help
Alright, I am pretty new to C++ and programming in general. In my class we just started to discuss Bitwise operations. The professor went over how things are stored in memory and what not. Then he gave us this assignment:
Write a program that generates 1000 random coin tosses and store them as bits in an unsigned integer. Then write a function that finds the result of the 9th toss. 1 = heads 0 = tails So I could easly write a program that generates 1000 random tosses, and store them in an array and grab the 9th toss. But he loses me when he wants us to store it as bits in an unsigned integer. Can someone please explain what and how exactly you store bits in an unsigned integer, and the works of that. The whole bits and memory thing was pretty confusing to me. I am not asking for someone to do this assignment for me, but just to help shed some light on the topic and make it more clear if at all possible. Thanks in advance for the help. |
|
#2
|
|||
|
|||
|
Hey!
Maybe he wants you to store either a 1 or a 0 as a Bit inside an Int data type. However, Int is composed by 16 bits...therefore, it's imposible to store 1000 bits in this type of data (you would need a 1000 bit data type to do so). I belive unsigned int is the type of data desired. if the teacher asks you to store them in an unsigned int, just declare: unsigned int tosses[1000]; If that's not what he wants....ask him how to store 1000 bits inside an Int data type!! Hope I'm right and it works for you!! Anibal. |
|
#3
|
|||
|
|||
|
Hey Anibal!
Thanks for the reply. I still have to talk to the prof. again to see exactly how he wanted this done. I know he said unsigned int, which is 32 bit correct? So I made an array of 32 elements, since 32x32 = 1024 which is enough to hold 1000 flips. It is almost working. I have the array holding the coin tosses. And now I have to have a function to get specific tosses. So I came up with this function that will determine the element of the array to look in according to the coin flip you want. Than I have it shifting the flip you want to check all the way to the right and then I am ANDing it with a mask which I have set to 0x1. This will then return the 1 or 0 for heads or tails. Hope that makes sense. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Bit help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|