How to print in binary

  Рет қаралды 16,426

CodeVault

CodeVault

Күн бұрын

Printing in binary format using the printf function is not as easy as it is with other bases (decimal, hexadecimal or octal) but can be done relatively easy with this algorithm.
Check out our Discord server: / discord

Пікірлер: 33
@eulerco.2036
@eulerco.2036 3 жыл бұрын
did you count your bits ...>>9 ??. maybe you better should shift from 7 to 0
@CodeVault
@CodeVault 3 жыл бұрын
Oh yea, now I see the mistake. It should be: for (j = 7; j >= 0; j--) That's why we're getting 9 bits per byte at the end. Thanks for catching that!
@rikkoo
@rikkoo Жыл бұрын
I love how the people who don't make coding videos are the first to correct other people's mistakes. Great tutorial, thanks
@CodeVault
@CodeVault Жыл бұрын
I still think it's valuable feedback. In a lot of the videos I omit certain aspects to make things easier to explain and understand (and possibly revisit later on). Sometimes it's good to have someone explain those aspects I omit in more detail in the comments below and, if the explanation is good, I end up pinning the comment
@danielesquivel3155
@danielesquivel3155 Жыл бұрын
@@CodeVault yes
@gimB83
@gimB83 3 жыл бұрын
Thanks a lot for this well explained guide - that's exactly what I needed.
@mitchanx1337
@mitchanx1337 3 жыл бұрын
big thanks. that was really helpful with my project
@manzoorelahitamjeed
@manzoorelahitamjeed 3 жыл бұрын
Great video. Helped me a lot.
@javierbravogutierrez5083
@javierbravogutierrez5083 Жыл бұрын
Man, you are awesome!! Million thanks
@harshdubey5763
@harshdubey5763 3 жыл бұрын
Don't you think this is going a bit overboard? You can simply right shift and use %2 operation to get the binary. Then store it in an array and print it. Regardless, your videos are very helpful. Thanks.
@CodeVault
@CodeVault 3 жыл бұрын
True. Although I didn't want to use any additional memory. You can share the code for that idea here and I'll pin it if you want. Your idea is very good!
@harshdubey5763
@harshdubey5763 3 жыл бұрын
@@CodeVault Also, shouldn't "j" in the 2nd for loop be set to 7? If you set it to 8 then you have shifted the character 8 places and basically lost the MSB. The code will work for smaller numbers but try to test it with 128.
@CodeVault
@CodeVault 3 жыл бұрын
Yeah, that's a mistake I made, my bad. I pinned a comment with the correct version of the for loop
@mrjadhav1
@mrjadhav1 2 жыл бұрын
Just keep two loops as it, and keep number untouched, AND with 1
@tempusFugit1337
@tempusFugit1337 3 жыл бұрын
Hi, very useful Vid but there is an additional mistake in it. The outer for loop has to be " for(i = (sizeof(int) - 1); i >= 0; i--){} ". Elsewise he will print out the bits of single bytes in the right order but the whole bytes mirrored. By the way this code also works fine for negative integers as long as the hardware system uses internal two's complement notation (which is the case in 99,9%). Greetings
@AnalogDude_
@AnalogDude_ 6 ай бұрын
Cool video
@Ca3yMiX
@Ca3yMiX 5 жыл бұрын
thanks for the Video, can u make a other one for Reverse Bits please.
@CodeVault
@CodeVault 5 жыл бұрын
Sure thing. Thanks for the suggestion!
@sepgh2216
@sepgh2216 5 ай бұрын
Great explanation. Some questions though. According to this, the representation of '-13' is: '11110011 11111111 11111111 11111111'. A) is it correct to assume that the first bit in first byte is for the signature? 1111001>1
@sepgh2216
@sepgh2216 5 ай бұрын
update: I checked this video about signed number binary representation and its all starting to make sense: kzfaq.info/get/bejne/Y6l1fbt1yqrXcYU.htmlsi=OeG2PcYKywU-uU9q&t=692
@jamesbela9719
@jamesbela9719 2 жыл бұрын
Thanks for the video but what if it is float or double numbers ?
@CodeVault
@CodeVault 2 жыл бұрын
You can use the same exact technique
@muhammadchaudhry4078
@muhammadchaudhry4078 3 ай бұрын
Hey Guys! While getting execution of printf command with%hhd to display the binary characters in Visual Studio Code, it only gives result in decimal like 13 and not in binary. Can someone help to get the result in binary bytes for integers, as shown in the video.. Thanks
@CodeVault
@CodeVault 3 ай бұрын
%hhd is decimal and I use it to print the 0s and 1s from a char data type (make sure that's what you are passing to printf). Otherwise you can use %x to print in hex for larger data types
@ishangupta8731
@ishangupta8731 2 жыл бұрын
#include #include #include /** * size of datatype (number of bytes) * typically the output of sizeof operator * pointer to 0th byte * in simple terms byte array */ void print_binary(size_t size, void* ip_data){ char *data = (char*) ip_data; long long i, j; for(i=0; i=0; j--){ char bit = (byte >> j) & 1; printf("%hhd", bit); } printf(" "); } } int main(int argc, char const *argv[]) { int a = 13; print_binary(sizeof(int), &a); printf(" "); return 0; }
@ishangupta8731
@ishangupta8731 2 жыл бұрын
@CodeVault is this solution to your question correct?
@CodeVault
@CodeVault 2 жыл бұрын
Yep, that is correct. Very good job!
@ishangupta8731
@ishangupta8731 2 жыл бұрын
@@CodeVault You are great teacher. Keep up the good work 😃
@emirabouzaki8001
@emirabouzaki8001 Жыл бұрын
i get a segfault when trying this, any advice?
@emirabouzaki8001
@emirabouzaki8001 Жыл бұрын
i'm trying to do it for the double datatype
@CodeVault
@CodeVault Жыл бұрын
You can send the code either here or on discord.code-vault.net
printf(MEMORY) | How to print memory to the console
12:18
CodeVault
Рет қаралды 9 М.
Reversing all the bits in an integer
15:18
CodeVault
Рет қаралды 12 М.
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 25 МЛН
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 12 МЛН
ОБЯЗАТЕЛЬНО СОВЕРШАЙТЕ ДОБРО!❤❤❤
00:45
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 66 МЛН
Oh, wait, actually the best Wordle opener is not “crane”…
10:53
I run untested, viewer-submitted code on my 500-LED christmas tree.
45:17
Bitwise Operators | C Programming Tutorial
12:19
Portfolio Courses
Рет қаралды 23 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 20 М.
Why do programmers use hexadecimal numbers?
8:41
Jacob Sorber
Рет қаралды 56 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Read and Write in Hexadecimal, The Easy Way!
6:36
Max's Tech
Рет қаралды 20 М.
Arrays as function parameters in C
13:28
CodeVault
Рет қаралды 12 М.
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 25 МЛН