Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

BINARY

Name: Anonymous 2010-11-10 18:20

So is this an efficient way to print out a 32bit little-endian integer in binary?


void printBinary(int number)
{
    char temp[33];
    char* p = &temp[32];
    int mask = 1;
    *p = 0;
    for (int i = 0; i < 32; ++i)
    {
        --p;
        *p = (number & mask) + 48;
        mask <<= 1;
    }
    puts(temp);
}

Name: Anonymous 2010-11-10 18:23

ihbt

Name: Anonymous 2010-11-10 18:24

Noticed something weird in the output, a thought error on my part, changed the *p assigment line to
*p = ((number & mask) > 0) + 48;

Name: Anonymous 2010-11-10 18:24

You forgot to manually terminate your string, ``faggot''.

Also,
= &temp[32];
What the fuck.

Name: Anonymous 2010-11-10 18:28

>>4
I fil the string backwards, the *p = 0; adds the null terminator.

Name: Anonymous 2010-11-10 18:30

Not particularly. But that doesn't matter. It's a pretty ugly one, though.

Name: Anonymous 2010-11-10 19:14

Undefined behavior detected: mask <<= 1 when mask is 16384 and INT_MAX is 32767.

Name: Anonymous 2010-11-10 19:18


#include <stdio.h>
#include <stdint.h>
 
void print_binary(uint32_t n)
{
    uint32_t mask = ~((uint32_t)~0 >> 1);
    do
        printf(n & mask ? "1" : "0");
    while (mask >>= 1);
}

Name: Anonymous 2010-11-10 19:19

>>7
your an idiot

Name: Anonymous 2010-11-10 19:28

>>8
Undefined behavior detected: ~0 when signed integers are represented with sign and magnitude and the implementation does not support negative zeros.

Name: Anonymous 2010-11-10 19:37

>>7,10
oh look!, some ``faggot'' that just discovered the term undefined behavior. too sad, he doesn't understand it.

Name: Anonymous 2010-11-10 19:37

>>10
get the fuck out already, nobody likes you

Name: Anonymous 2010-11-10 19:46

>>7,10
Undefined behavior detected: being a fucking faggot.

Name: Anonymous 2010-11-10 20:55

>>13
Seems pretty well-defined to me.

Name: Anonymous 2010-11-12 12:32

section .text
    global _start

cprint:
    xor rax, rax
    inc rax
    mov rdi, rax
    lea rsi, [rsp + 8]
    mov rdx, rax
    syscall
    ret

bprint:
    xor r8, r8
    inc r8
    ror r8, 1
    mov r9, r8
    mov rbx, [rsp + 8]
lewp:
    mov rcx, rbx
    and rcx, r8
    test rcx, rcx
    jz .zero
    push '1'
    jmp .one
.zero:
    push '0'
.one:
    call cprint
    add rsp, 8
    shr r8,  1
    test r8, r8
    jnz lewp
    ret

_start:
    push 0xbaf
change this ``faggot''
    call bprint
    mov qword [rsp], 0xA
    call cprint
    add rsp, 8
    mov rax, 60
    mov rdi, 0
    syscall

Don't change these.
Name: Email:
Entire Thread Thread List