You are not logged in.

#1 2006-10-31 18:14:21

orjanp
Member
From: Tromsoe, Norway
Registered: 2004-07-03
Posts: 347

C: Printing bits.

If I have an integer with the value, lets say 2. How can I print the bit representation to screen?

Orjanp...


Ørjan Pettersen

Offline

#2 2006-10-31 22:37:58

tranquility
Member
From: Portugal
Registered: 2004-08-06
Posts: 136

Re: C: Printing bits.

The only way I know of is going bit by bit and applying a bit mask that shows the value of that bit, like your_integer & bit_mask_for_bit_X.

Offline

#3 2006-10-31 22:52:07

tardo
Member
Registered: 2006-07-15
Posts: 526

Re: C: Printing bits.

edit: ok wtf it won't let me print it

basically have a for loop uptil 32 printing out your number shifted right each iterationg, num >> i, where i = 0 .. 31

Offline

#4 2006-11-01 08:40:53

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: C: Printing bits.

you can print it as hex or octal - and from there conversion to bits is easy smile

Offline

#5 2006-11-02 14:00:34

orjanp
Member
From: Tromsoe, Norway
Registered: 2004-07-03
Posts: 347

Re: C: Printing bits.

This gave me a itoa function. Works like a charm. smile

Orjanp...


Ørjan Pettersen

Offline

Board footer

Powered by FluxBB