Questions tagged [binary]
Binary, the base-2 numeral system, represents numbers using two symbols: 0 and 1. For compiled computer programs, use the "executable" tag instead.
binary
14,808
questions
1407
votes
12
answers
1.2m
views
What does the 'b' character do in front of a string literal?
Apparently, the following is the valid syntax:
b'The string'
I would like to know:
What does this b character in front of the string mean?
What are the effects of using it?
What are appropriate ...
1013
votes
66
answers
648k
views
Count the number of set bits in a 32-bit integer
8 bits representing the number 7 look like this:
00000111
Three bits are set.
What are the algorithms to determine the number of set bits in a 32-bit integer?
817
votes
36
answers
1.2m
views
Convert int to binary string in Python
How do I convert an integer into a binary string in Python?
37 → '100101'
514
votes
23
answers
486k
views
What is “two's complement”?
I'm in a computer systems course and have been struggling, in part, with two's complement. I want to understand it, but everything I've read hasn't brought the picture together for me. I've read the ...
453
votes
13
answers
1.2m
views
Reading binary file and looping over each byte [duplicate]
In Python, how do I read in a binary file and loop over each byte of that file?
444
votes
13
answers
161k
views
Why do we use Base64?
Wikipedia says
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This ...
431
votes
3
answers
654k
views
How to convert 'binary string' to normal string in Python3? [duplicate]
For example, I have a string like this(return value of subprocess.check_output):
>>> b'a string'
b'a string'
Whatever I did to it, it is always printed with the annoying b' before the ...
407
votes
8
answers
341k
views
How do you express binary literals in Python?
How do you express an integer as a binary number with Python literals?
I was easily able to find the answer for hex:
>>> 0x12AF
4783
>>> 0x100
256
and octal:
>>> 01267
...
392
votes
13
answers
575k
views
How to view files in binary from bash?
I would like to view the contents of a file in the current directory, but in binary from the command line. How can I achieve this?
317
votes
13
answers
493k
views
How to print (using cout) a number in binary form?
I'm following a college course about operating systems and we're learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers ...
274
votes
15
answers
338k
views
How to compare binary files to check if they are the same?
What is the easiest way (using a graphical tool or command line on Ubuntu Linux) to know if two binary files are the same or not (except for the time stamps)? I do not need to actually extract the ...
270
votes
17
answers
518k
views
Converting integer to binary in Python
In order to convert an integer to a binary, I have used this code:
>>> bin(6)
'0b110'
and when to erase the '0b', I use this:
>>> bin(6)[2:]
'110'
What can I do if I want to show 6 ...
244
votes
24
answers
486k
views
Can I use a binary literal in C or C++?
I need to work with a binary number.
I tried writing:
const char x = 00010000;
But it didn't work.
I know that I can use a hexadecimal number that has the same value as 00010000, but I want to know ...
240
votes
11
answers
296k
views
Convert to binary and keep leading zeros
I'm trying to convert an integer to binary using the bin() function in Python. However, it always removes the leading zeros, which I actually need, such that the result is always 8-bit:
Example:
bin(...
240
votes
19
answers
127k
views
Why prefer two's complement over sign-and-magnitude for signed numbers?
I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1?
-1 is represented by 11111111 (two's complement) rather than (to ...
238
votes
12
answers
199k
views
C# binary literals
Is there a way to write binary literals in C#, like prefixing hexadecimal with 0x? 0b doesn't work.
If not, what is an easy way to do it? Some kind of string conversion?
224
votes
14
answers
297k
views
Convert a binary NodeJS Buffer to JavaScript ArrayBuffer
How can I convert a NodeJS binary buffer into a JavaScript ArrayBuffer?
214
votes
17
answers
151k
views
Why does Git treat this text file as a binary file?
I wonder why git tells me this?
$ git diff MyFile.txt
diff --git a/MyFile.txt b/MyFile.txt
index d41a4f3..15dcfa2 100644
Binary files a/MyFile.txt and b/MyFile.txt differ
Aren't they text files?
I ...
209
votes
19
answers
388k
views
Converting an int to a binary string representation in Java?
What would be the best way (ideally, simplest) to convert an int to a binary string representation in Java?
For example, say the int is 156. The binary string representation of this would be "...
208
votes
6
answers
441k
views
Tool for comparing 2 binary files in Windows [closed]
I need a tool to compare 2 binaries. The files are quite large. Some freeware or trial tools I found on the Internet are not convenient to use for large files. Can you recommend me some tools?
195
votes
8
answers
648k
views
Convert decimal to binary in python [duplicate]
Is there any module or function in python I can use to convert a decimal number to its binary equivalent?
I am able to convert binary to decimal using int('[binary_value]',2), so any way to do the ...
195
votes
8
answers
156k
views
How to convert a string or integer to binary in Ruby?
How do you create integers 0..9 and math operators + - * / in to binary strings.
For example:
0 = 0000,
1 = 0001,
...
9 = 1001
Is there a way to do this with Ruby 1.8.6 without using a library?...
178
votes
11
answers
310k
views
Are the shift operators (<<, >>) arithmetic or logical in C?
In C, are the shift operators (<<, >>) arithmetic or logical?
177
votes
13
answers
152k
views
Fast way of counting non-zero bits in positive integer
I need a fast way to count the number of bits in an integer in python. My current solution is
bin(n).count("1")
but I am wondering if there is any faster way of doing this?
171
votes
10
answers
598k
views
How to convert string to binary?
I am in need of a way to get the binary representation of a string in python. e.g.
st = "hello world"
toBinary(st)
Is there a module of some neat way of doing this?
168
votes
8
answers
492k
views
Reading a binary file with python
I find particularly difficult reading binary file with Python. Can you give me a hand?
I need to read this file, which in Fortran 90 is easily read by
int*4 n_particles, n_groups
real*4 group_id(...
155
votes
12
answers
286k
views
How to convert a Binary String to a base 10 integer in Java
I have an array of Strings that represent Binary numbers (without leading zeroes) that I want to convert to their corresponding base 10 numbers. Consider:
binary 1011 becomes integer 11
binary 1001 ...
154
votes
23
answers
505k
views
Convert hex to binary
I have ABC123EFFF.
I want to have 001010101111000001001000111110111111111111 (i.e. binary repr. with, say, 42 digits and leading zeroes).
How?
154
votes
8
answers
433k
views
Reading and writing binary file
I'm trying to write code to read a binary file into a buffer, then write the buffer to another file. I have the following code, but the buffer only stores a couple of ASCII characters from the first ...
152
votes
17
answers
169k
views
How to get 0-padded binary representation of an integer in java?
for example, for 1, 2, 128, 256 the output can be (16 digits):
0000000000000001
0000000000000010
0000000010000000
0000000100000000
I tried
String.format("%16s", Integer.toBinaryString(1));
it puts ...
149
votes
21
answers
45k
views
Is it safe to use -1 to set all bits to true?
I've seen this pattern used a lot in C & C++.
unsigned int flags = -1; // all bits are true
Is this a good portable way to accomplish this? Or is using 0xffffffff or ~0 better?
131
votes
21
answers
108k
views
How can I detect if a file is binary (non-text) in Python?
How can I tell if a file is binary (non-text) in Python?
I am searching through a large set of files in Python, and keep getting matches in binary files. This makes the output look incredibly messy.
I ...
129
votes
8
answers
56k
views
binary protocols v. text protocols
does anyone have a good definition for what a binary protocol is? and what is a text protocol actually? how do these compare to each other in terms of bits sent on the wire?
here's what wikipedia ...
126
votes
3
answers
268k
views
Remove 'b' character do in front of a string literal in Python 3 [duplicate]
I am new in python programming and i am a bit confused. I try to get the bytes from a string to hash and encrypt but i got
b'...'
b character in front of string just like the below example. Is any ...
120
votes
14
answers
267k
views
How to convert a byte to its binary string representation
For example, the bits in a byte B are 10000010, how can I assign the bits to the string str literally, that is, str = "10000010".
Edit
I read the byte from a binary file, and stored in the ...
118
votes
4
answers
106k
views
How to get only the first ten bytes of a binary file
I am writing a bash script that needs to get the header (first 10 bytes) of a file and then in another section get everything except the first 10 bytes. These are binary files and will likely have \0'...
115
votes
10
answers
271k
views
How can I perform math operations on binary numbers?
How can I add, subtract, and compare binary numbers in Python without converting them to decimal?
114
votes
12
answers
123k
views
How do you embed binary data in XML?
I have two applications written in Java that communicate with each other using XML messages over the network. I'm using a SAX parser at the receiving end to get the data back out of the messages. ...
113
votes
17
answers
155k
views
Bitwise operation and usage
Consider this code:
x = 1 # 0001
x << 2 # Shift left 2 bits: 0100
# Result: 4
x | 2 # Bitwise OR: 0011
# Result: 3
x & 1 # Bitwise AND: 0001
# Result: 1
I can ...
111
votes
9
answers
88k
views
How to remove unused objects from a git repository?
I accidentally added, committed and pushed a huge binary file with my very latest commit to a Git repository.
How can I make Git remove the object(s) that was/were created for that commit so my .git ...
110
votes
22
answers
245k
views
Count number of 1's in binary representation
Efficient way to count number of 1s in the binary representation of a number in O(1) if you have enough memory to play with. This is an interview question I found on an online forum, but it had no ...
109
votes
32
answers
533k
views
C++ - Decimal to binary converting
I wrote a 'simple' (it took me 30 minutes) program that converts decimal number to binary. I am SURE that there's a lot simpler way so can you show me?
Here's the code:
#include <iostream>
#...
108
votes
9
answers
94k
views
Converting from an integer to its binary representation
Has anyone got an idea if there is any inbuilt functionality in Go for converting from any one of the numeric types to its binary number form.
For example, if 123 was the input, the string "1111011" ...
107
votes
4
answers
145k
views
How to append binary data to a buffer in node.js
I have a buffer with some binary data:
var b = new Buffer ([0x00, 0x01, 0x02]);
and I want to append 0x03.
How can I append more binary data? I'm searching in the documentation but for appending ...
103
votes
6
answers
121k
views
Compare two Byte Arrays? (Java)
I have a byte array with a ~known binary sequence in it. I need to confirm that the binary sequence is what it's supposed to be. I have tried .equals in addition to ==, but neither worked.
byte[] ...
103
votes
7
answers
187k
views
Reading integers from binary file in Python
I'm trying to read a BMP file in Python. I know the first two bytes
indicate the BMP firm. The next 4 bytes are the file size. When I execute:
fin = open("hi.bmp", "rb")
firm = fin.read(2)
...
98
votes
8
answers
318k
views
Convert binary to ASCII and vice versa
Using this code to take a string and convert it to binary:
bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in 'hello'), 0))
this outputs:
0b110100001100101011011000110110001101111
Which, if I put ...
97
votes
4
answers
138k
views
Flask to return image stored in database
My images are stored in a MongoDB, and I'd like to return them to the client, here is how the code is like:
@app.route("/images/<int:pid>.jpg")
def getImage(pid):
# get image binary from ...
97
votes
14
answers
124k
views
Binary representation of float in Python (bits not hex)
How can I get a string of 0s and 1s, according to the bits of the IEEE 754 representation of a 32 bit float?
For example, given an input 1.00, the result should be '00111111100000000000000000000000'.
95
votes
9
answers
61k
views
How can I convert a binary file to the text declaring a C/C++ array with that content?
I need to include the contents of a binary file in my C/C++ source code as the text for the declaration of an array initialized to the content of the file. I'm not looking to read the file dynamically ...