Re: [Dev-C++] Decrementing for loop problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2005-07-17 09:29:20
|
Note that it is easier to do direct bit manipulations using the shift
operator.
unsigned char val = 0x25; // number to print
mask = 0x80; // start from most significant bit
while (mask) {
printf("%d",(val & mask) != 0);
mask >>= 1;
}
Also don't forget that checking a specific bit in a number is just:
if (num & (1 << bitpos)) {
...
}
Setting the bit is:
num |= 1 << bitpos;
Clearing it is:
num &= ~(1 << bitpos);
/Per W
On Sat, 16 Jul 2005, Roger Furer wrote:
> Aloha from Honolulu,
> I have a little c program for studying bit operators. I'm trying to
> print out a byte in binary form. My first 'for' loop increments the
> index 'i' and works fine, but the number reads backwards. I tried the
> same thing but with an index that is decremented 'x--', but the loop
> never executes. Can anyone help?
> Here is the program:
|