-
August 13th, 2022, 11:12 AM
#1
New with C programming, question about a loop situation
Hi all,
I'm new to the C language, trying to do an exercice where I have to print the "fifth" bit of a given number (0 to 255).The condition of this exercice is to not use operators of affectation (such as >>x or <<x, etc.).
I need to do it with modulo and divisions.
I'm really blocked, tried many things, also searched for solutions on the web, but no success so far. If anyone could guide threw, I would be thankful as heck.
Pasting my poorly written code, then I will do a comment as what is my idea behind this.
int main(void)
{
int number_user;
int bit;
int i;
printf("Please put an integer between 0 and 255: ");
scanf("%d", &number_user);
i = 0;
while (i <= 4)
{
number_user /= 2;
i++;
}
bit = number_user % 2;
printf ("your fifth bit is %d: \\n", bit);
return EXIT_SUCCESS;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|