Yes, your showbits() is correct too. There is a small thing you should pay attention using right shifts on signed integer types: There is 2 types of right shifts: arithmetic and logical. Probably the compiler will use the arithmetic kind, but the standard says this is "implementation defined". In your routine this doesn't matter, since you are masking all bits except bit 0, after the shift.
The difference between my implementation and yours is that I prefer to mask all but the most significant bit, shifting left 1 bit per loop iteration, because I learned to avoid that "implementation defined" behavior, for the sake of portability (that's why I use sizeof(int)*8 and 8*sizeof x to get bit counts. I could use the constant 32).
This is not a criticism, as I said, your routine is correct! But mine, on x86-64, is a little better (side by side listing in assembly):
Code:
showbits_rm82co: showbits_fred:
push rbp push rbp
push rbx push rbx
mov ebp, edi mov ebp, edi
mov ebx, 31 mov ebx, 32
sub rsp, 8 sub rsp, 8
jmp .L4 .L12:
.L9: mov edi, ebp
mov edi, '0' mov rsi, QWORD [rip+stdout]
sub ebx, 1 add ebp, ebp
call putchar@PLT shr edi, 31
cmp ebx, -1 add edi, '0'
je .L8 call _IO_putc@PLT
.L4: sub ebx, 1
bt ebp, ebx jne .L12
jnc .L9 add rsp, 8
mov edi, '1' pop rbx
sub ebx, 1 pop rbp
call putchar@PLT ret
cmp ebx, -1
jne .L4
.L8:
add rsp, 8
pop rbx
pop rbp
ret
Notice, in my routine, there is only one call to _IO_putc (more efficient than putchar) and less jumps... You can archive the same result making this change:
Code:
// bit == 0 ? printf("0") : printf("1");
putchar( bit == 0 ? '0' : '1' );
PS: YOUR routine is way more efficient than mine on ARM!
Code:
showbits_rm82co: showbits_fred:
push {r4, r5, r6, lr} push {r4, r5, r6, lr}
mov r5, r0 mov r5, r0
mov r4, #31 ldr r6, .L19
.L4: mov r4, #32
asr r3, r5, r4 b .L14
mov r0, #48 .L11:
tst r3, #1 ldr r3, [r2]
movne r0, #49 subs r4, r4, #1
bl putchar lsl r5, r5, #1
subs r4, r4, #1 add r0, r3, #1
bcs .L4 str r0, [r2]
pop {r4, r5, r6, lr} strb r1, [r3]
bx lr beq .L18
.L14:
ldr r0, [r6]
cmp r5, #0
movlt r1, #49
movge r1, #48
ldr r2, [r0, #8]
ldr r3, [r2, #8]
sub r3, r3, #1
cmp r3, #0
str r3, [r2, #8]
bge .L11
ldr ip, [r2, #24]
cmp r3, ip
bge .L11
bl __swbuf_r
subs r4, r4, #1
lsl r5, r5, #1
bne .L14
.L18:
pop {r4, r5, r6, lr}
bx lr
[]s
Fred