d326: 程式設計師的面試問題(二)
Tags :
Accepted rate : 647人/675人 ( 96% ) [非即時]

Content

==============================================

v是介於-2147483647到2147483647的整數值.
a是代表這個整數的第a個bit.
b如果是1,表示要將v的第a bit設成1.
b如果是0,表示要將v的第a bit設成0.

==============================================

#include <stdio.h>

/* set bit b to 1 */
int set_bit(int v, int b)
{

}

/* set bit b to 0 */
int unset_bit(int v, int b)
{

}

/* check_bit b is 1 or 0 */
int check_bit(int v, int a, int b)
{

}

int main(void)
{
int i, v, bit, isSet;

while(scanf("%d %d %d",&v,&bit,&isSet)==3)
{
if(isSet)
v = set_bit(v, bit);
else
v = unset_bit(v, bit);

for(i=31;i>=0;--i)
printf( "%d", check_bit(v, 32, i) );
putchar('\n');
}

return 0;
}

Input

v的範圍為-2147483647到2147483647.
a的範圍為0到31.
b不是0就是1.

Output

Sample Input #1
0 0 1
1 0 0
2147483647 31 1
-1 31 0
4 0 1
4 0 0
Sample Output #1
00000000000000000000000000000001
00000000000000000000000000000000
11111111111111111111111111111111
01111111111111111111111111111111
00000000000000000000000000000101
00000000000000000000000000000100

Hint ：

Tags:

[管理者：
(Bleed)
]

 ID User Problem Subject Hit Post Date 16378 (帥氣魔方生) d326 741 2018-12-26 01:02