You are welcome. Tell us how you ended up solving the problem of keeping the order of the letters as they came in without using an array.
20 Replies - 2294 Views - Last Post: 06 June 2013 - 02:43 PM
#17
Re: Lower and Upper Case in C language
Posted 05 June 2013 - 07:36 PM
I am new to bit shifting so don't know how to manipulate them very well. Now to output each bit from the variable int flag.
int i;
for(i = 0; i < 26; i++)
{
if((flag & 1) >= 1)
{
printf("%c", i + 'a');
}
}
Would this work? AND my flag with 1, and if true print i + 'a'?
I am new to bit shifting so don't know how to manipulate them very well. Now to output each bit from the variable int flag.
int i;
Would this work? AND my flag with 1, and if true print i + 'a'?
int i;
for(i = 0; i < 26; i++)
{
if((flag & 1) >= 1)
{
printf("%c", i + 'a');
}
}
Would this work? AND my flag with 1, and if true print i + 'a'?
I am new to bit shifting so don't know how to manipulate them very well. Now to output each bit from the variable int flag.
int i;
for(i = 0; i < 26; i++)
{
if((flag & 1) >= 1)
{
printf("%c", i + 'a');
}
}
Would this work? AND my flag with 1, and if true print i + 'a'?
#18
Re: Lower and Upper Case in C language
Posted 05 June 2013 - 07:44 PM
Sorry I meant AND it with 2 to the power of 1,2,3,4,5,6
#19
Re: Lower and Upper Case in C language
Posted 05 June 2013 - 08:03 PM
Quote
printf("%c", i + 'a');
Since the information is held in the flag variable, your print statement should the flag variable don't you think?
To print each bit in an integer, loop over the total number of bits in the flag (0 - sizeof(int) * 8). For each iteration, shift the flag variable to the right by the value of 'i', then bitwise-and the result with 1.
i.e.
#define BITS_PER_BYTE 8
int flag = 0x22; //binary = 0000 0000 0010 0010 (32 bit integer)
for(int i=0; i<sizeof(int) * BITS_PER_BYTE; i++) {
printf("%d, (flag >> i) & 1);
}
The effect of bitwise-anding the result of (flag >> i) makes all the other bits 0, except the one that is of current intrest.
You will notice that the code above, when run, will print the binary backwards (most significant bit last). This is because we start print the least significant bit first (flag >> 0) & 1.
This post has been edited by jjl: 05 June 2013 - 08:05 PM
#20
Re: Lower and Upper Case in C language
Posted 05 June 2013 - 11:23 PM
jjl, on 05 June 2013 - 08:03 PM, said:
Quote
printf("%c", i + 'a');
Since the information is held in the flag variable, your print statement should the flag variable don't you think?
To print each bit in an integer, loop over the total number of bits in the flag (0 - sizeof(int) * 8). For each iteration, shift the flag variable to the right by the value of 'i', then bitwise-and the result with 1.
i.e.
#define BITS_PER_BYTE 8
int flag = 0x22; //binary = 0000 0000 0010 0010 (32 bit integer)
for(int i=0; i<sizeof(int) * BITS_PER_BYTE; i++) {
printf("%d, (flag >> i) & 1);
}
The effect of bitwise-anding the result of (flag >> i) makes all the other bits 0, except the one that is of current intrest.
You will notice that the code above, when run, will print the binary backwards (most significant bit last). This is because we start print the least significant bit first (flag >> 0) & 1.
I understand what you are saying, but I have to print the character that is mapped to the bit. Which is why my printf had a char output. Which is why I used i + 'a' to output the character corresponding to the current bit.
My goal was to check if the bit is 1 or 0. If it is 1 then output the alphabet thats mapped to it.
#21
Re: Lower and Upper Case in C language
Posted 06 June 2013 - 02:43 PM
Quote
My goal was to check if the bit is 1 or 0. If it is 1 then output the alphabet thats mapped to it.
Well you know how to get the status of each bit, I showed it in the previous post.

New Topic/Question
Reply



MultiQuote

|