Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing a bit value in a binary file

Posted on 1999-01-05
9
Medium Priority
?
211 Views
Last Modified: 2010-04-02
Hi,

I have pulled out my hair trying to figure this out.  I've got the following code which finds a unique number within a bitmap file, and lets the calling method know if the bit representation is turned on or off:

byte valByte;
ifstream readMap (BMP_FILE, ios::in|ios::binary);

if (readMap) {
  // param is a 6-digit number string
  uint16 bitNum = atoi ((char *) &param);
  uint32 byteOffset = bitNum / 8;
  uint16 bitOffset  = bitNum % 8;
  readMap.seekg (byteOffset, ios::beg);
  // Error checking deleted
  readMap.seekg (0, ios::end);
  // Error checking deleted
  readMap.read (&valByte, 1);
  if (valByte & (int)pow (2, bitOffset) && readMap)
    // This bit is turned on
  else
    // This bit is turned off

I now need to be able to flip the bit value.  I was thinking I could try something like this:

if ((valByte & (int)pow (2, bitOffset)) == 0) {
  // If the bit is off, turn it on
  (valByte & (int)pow (2, bitOffset)) = 1;
} else {
  // The card must be on, turn it off
  (valByte & (int)pow (2, bitOffset)) = 0;
};
// Now save the byte back to the file

But for obvious reasons (no Lvalue), this won't work.

For information, this code will only be run on PC-based (8-bit byte based) machines.  Any help would be immensely appreciated!!
0
Comment
Question by:cgoldfarb
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1181605
valByte ^= (1<<bitOffset);
0
 
LVL 7

Expert Comment

by:faster
ID: 1181606
ozo means to replace all pow (2, bitOffset) with (1<<bitOffset) and for flip the bit value, use
valByte ^= (1<<bitOffset); this single line does the job of your if/else block.
0
 

Author Comment

by:cgoldfarb
ID: 1181607
Thank you both for your help.  It's exactly what I was looking for.  Will hopefully return the favor...
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.

 

Accepted Solution

by:
erick1217 earned 600 total points
ID: 1181608
try to turning on
valByte |= (int)pow (2, bitOffset);

and to turning off

valByte &= 256 - (int)pow (2, bitOffset);



0
 
LVL 84

Expert Comment

by:ozo
ID: 1181609
255-pow may work better than 256-pow
but it's still pretty silly to use transcendental functions to approximate the << operator
0
 

Expert Comment

by:erick1217
ID: 1181610
sorry ozo but the power of bit 4 (by example) is 8
and 256 - 8 = 248
valByte &= 248 will set the bit 4 to zero
0
 
LVL 84

Expert Comment

by:ozo
ID: 1181611
valByte &= 248 will set bits 0, 1, and 2 to zero
0
 

Expert Comment

by:erick1217
ID: 1181612
yes.. you're right... excuse me...
0
 

Author Comment

by:cgoldfarb
ID: 1181613
erick1217's answer was good, although ozo and faster hit the nail on the head.  They really deserve the points, as they were the first to respond.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

609 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