Solved

Set bits

Posted on 2006-07-01
6
309 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 400 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 100 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 136
SQL Statment to match two tables in c# 6 78
How to learn Linux? 10 60
If a cell in a range equals "YES" return specific Test. 4 27
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now