C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 24th, 2005, 10:55 PM
Programmer1199 Programmer1199 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 4 Programmer1199 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 17 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old January 25th, 2005, 09:33 PM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
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.

Reply With Quote
  #3  
Old January 26th, 2005, 02:32 AM
Programmer1199 Programmer1199 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 4 Programmer1199 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 17 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Bit help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT