declare

How to print out a number in binary format using C/C++



Sharing buttons:

hey guys how are you today it's small

from programmer tube today I want to

show you how to print out a number in

binary format in C C++ so if you're

interested let's jump right into it okay

so here I am on Visual Studio this

create a new project by clicking on file

new project make sure you have 132

selected from this list and then select

132 console application it's called a

Trent binary click OK on this page just

click finish ok let's see how we can

print out an integer as a binary format

we're gonna create a function let's call

it integer to binary we're gonna give it

the integer we want to print out and a

character buffer that will contain the

result first thing we always do is check

for null pointers if null is P then we

can do anything you should return from

this function okay so let's do a

do-while loop here do-while our numbers

greater than zero if the remainder of

dividing the number by two and modulus

two is zero we should take contents of P

equals 0 in this case else it's 1 so we

can say a contents of B equals 1 and

then we can say P plus plus then N

equals and divided by 2 so now at the

very end we should say a contents of P

equals the null termination character

this is all assuming that the buffer we

have is sufficient enough to get the

result

ok so let's give this a try let's say

here in Maine let's assume we have a

char view of 100 that's a big enough

buffer we don't need that many bytes but

just to be safe or integer I equals 0

eyes this then let's say 256 plus plus I

sprint all the numbers from 0 to 255 I'm

gonna say now integers to binary I it

would be that should give us the value

now we can print it out printf or Sindhi

equals person s i and then let's run

this it's compile and run I'm gonna hit

ctrl f5 on the keyboard like yes so as

you can see here the numbers are

actually converting fine the only thing

is that

if you look at this number here it's

actually in reverse order to verify I

can launch my calculator here say 253 as

you can see here my numbers actually

reversed so one more step we need is to

reverse the numbers that we create so

let's go here include Strangler edge and

then let's keep this point or another

variable so we can point to the

beginning of the string then here the

very end we're gonna say STR Rev est R

so this would reverse our strings

okay so ctrl v again to compile and run

so now as you can see here the numbers

are actually now showing up correctly so

that's how we convert an integer to a

binary and C C++ there are many other

ways to do it but this is just one

example of doing it I hope you like this

video please don't forget to subscribe

to my channel share with your friends

thanks for watching

you

you