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.20363 жыл бұрын
did you count your bits ...>>9 ??. maybe you better should shift from 7 to 0
@CodeVault3 жыл бұрын
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 Жыл бұрын
I love how the people who don't make coding videos are the first to correct other people's mistakes. Great tutorial, thanks
@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 Жыл бұрын
@@CodeVault yes
@gimB833 жыл бұрын
Thanks a lot for this well explained guide - that's exactly what I needed.
@mitchanx13373 жыл бұрын
big thanks. that was really helpful with my project
@manzoorelahitamjeed3 жыл бұрын
Great video. Helped me a lot.
@javierbravogutierrez5083 Жыл бұрын
Man, you are awesome!! Million thanks
@harshdubey57633 жыл бұрын
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.
@CodeVault3 жыл бұрын
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!
@harshdubey57633 жыл бұрын
@@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.
@CodeVault3 жыл бұрын
Yeah, that's a mistake I made, my bad. I pinned a comment with the correct version of the for loop
@mrjadhav12 жыл бұрын
Just keep two loops as it, and keep number untouched, AND with 1
@tempusFugit13373 жыл бұрын
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_6 ай бұрын
Cool video
@Ca3yMiX5 жыл бұрын
thanks for the Video, can u make a other one for Reverse Bits please.
@CodeVault5 жыл бұрын
Sure thing. Thanks for the suggestion!
@sepgh22165 ай бұрын
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
@sepgh22165 ай бұрын
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
@jamesbela97192 жыл бұрын
Thanks for the video but what if it is float or double numbers ?
@CodeVault2 жыл бұрын
You can use the same exact technique
@muhammadchaudhry40783 ай бұрын
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
@CodeVault3 ай бұрын
%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
@ishangupta87312 жыл бұрын
#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; }
@ishangupta87312 жыл бұрын
@CodeVault is this solution to your question correct?
@CodeVault2 жыл бұрын
Yep, that is correct. Very good job!
@ishangupta87312 жыл бұрын
@@CodeVault You are great teacher. Keep up the good work 😃
@emirabouzaki8001 Жыл бұрын
i get a segfault when trying this, any advice?
@emirabouzaki8001 Жыл бұрын
i'm trying to do it for the double datatype
@CodeVault Жыл бұрын
You can send the code either here or on discord.code-vault.net