Solved

Set bits

Posted on 2006-07-01
6
306 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

746 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

16 Experts available now in Live!

Get 1:1 Help Now