[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Set bits

Posted on 2006-07-01
6
Medium Priority
?
316 Views
Last Modified: 2010-04-15
I would like to set each bit in a byte, then write the byte out to a binary file.
I then need to take a value from 0 - 12, convert it to 4 bits, then write the 4 bits out to a file.

Could someone provide me with some sample code?

This is running on a linux platform.  Thanks!
0
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1600 total points
ID: 17025156
Hi jewee,

The clearest way to set all bits in a byte is to set it to -1 (with casts if necessary).

I am sure you can do a loop from 0 to 12.

Writing 4 bits to a file is impossible, file writing is restricted to a minimum of 8-bit bytes. Are you expected to write these bytes out as '1'/'0' characters?

Paul
0
 

Author Comment

by:jewee
ID: 17025162
For the 4 bit value - there is also an additional 4 bit value following this.

I don't need a loop from 0 - 12.  I need to have a function which will take in a value ranging from 0 - 12, and convert it to 4 bits.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17025263
jewee,

A number taking 4 bits can range between 0 and 15. You already have only 4 bits! Why is this difficult?

Have a go at some code and post it describing your difficulties.

Paul
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:dark_archon
ID: 17025382
Paul's right; the number from 0-12 will already be using no more than four bits. If you want to convert it to ASCII, just do a loop from 0 to 3 and perform an "AND" against the number with one bit set depending on the value of the counter in the loop.
0
 

Assisted Solution

by:dark_archon
dark_archon earned 400 total points
ID: 17025401
And to output the two four-bit values to the file, do something like the following:

fputc( ( a << 4 ) | ( b & 0x0F ), fpout );

The left shift by four bits moves the value, a, into the higher "nibble", eliminating the four bits that were previously there. The b & 0x0F clears the higher four bits, leaving the lower four bits in place. And "OR" operation merges them together into the one byte.
0
 

Author Comment

by:jewee
ID: 17025514
Paul,

Sorry, I originally misunderstood your question.  thank you.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question