Calling all BIT Twiddlers....

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i got a feeling io'm gona punch myself in the face after i see the answer to this, because it was so simple. so here goes....

I have a 16 bit number that is a global variable, we will call it global1. I want to put this into a function so that whenever I want to change a bit or two I can call this function with the bit to change. Here is a small example.
now, the below is not elegant, but you get the idea. the first function call changes bit 4 to a 1 and the second call changes bit 4 to a 0. it does this without affecting any of the other bits in there.

Now, what I am searching for since the obvious has not hit me is a nice formula to do the same thing without the "IF" statements.

Thanks Guys.

ChangeBit(1,4);
ChangeBit(0,4);

void ChangeBit(char data, unsigned int rotating)
{
if(data){
global1 |= (1 << rotating);
}
else {
global1 &=~ (1 << rotating);
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

global1 &=~ (1 << rotating);
global1 |= (data << rotating);

:-) ?

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jimparisi,

How about the following:

Macro definition:

#define SetBit(x,y) (x|=(1<<y))
#define ClrBit(x,y) (x &= ~(1<<y))
#define ToggleBit(x,y) (x^=(1<<y))
#define TestBit(x,y) (x&(1<<y))

Usage:

main()
{
char Global = 0;

SetBit(Global, 4);

ClrBit(Global,4);

ToggleBit(Global,4);

if(TestBit(Global,4);)
}
// do this
}
else
{
// do something else
}
}

Compiler:

ImageCraft 6.xx & 7.xx

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, man... i totaly forgot that i had thoes defines in my header file.....
Thank you so much for opening my eye.

Jim

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jimparisi,

You might not have those macros defined any where in your compliers header files. The fact that they might actually be pre-definded might just be at the pleasure of the compiler writers.

ImageCraft ICCAVR doesn't have them. I had to define them. Err... Should I say, I copied them from the version 7.xx manual.

Glad I could spring your memory into spontainious recall.

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A long time ago I remember seeing those here on this forum. back then, I put them into a file called macros.h. Since that time, marcos.h has always just been included in all my programs, just as part of my typical cut and paste header files. after thinking about it for a bit I was wondering if there was a one line statement much like those that could handle it alone. I.E. Instead of calling SetBit or ClrBit I could call something like ChangeBitTo which would be a one line statement that could handle the setting or clearing of a bit in an int without using and if then statements to check for the bit to change condition.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you have to make some sort of decision, in order to determine what operation you required the function to perform.

To make a decision between two or more possibilities, the if-then-elsewould be used.

The switch-case could be used, as well.

But how do you decide between two or more operations without some sort of decision statement?

I prefer calling the indivudual macros. It just seems much more descriptive and, even thouth functionality is the goal, clearity is every bit as important.

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i agree with you that i would need to make a decison. i was just curious if there was somthing i just never thoguht of that could handle it without the if then statement or clearing the bit first just in case then seting it if needed to. Somthing whacky like

register1 *= ^ (1<< data) ^ (register1 % data) & (data << shift_bits);