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 is the base-2 positional numeral system. It represents numbers using two symbols: 0 and 1.

Computers use binary in digital circuitry using logic gates, where the symbols translate to states of off (0) and on (1).

Here are binary numbers corresponding to 0 to 10 in the decimal system:

Decimal   Binary
-------   ------
      0        0
      1        1
      2       10
      3       11
      4      100
      5      101
      6      110
      7      111
      8     1000
      9     1001
     10     1010

Other tags

14710 questions
1356
votes
12 answers

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 situations to use it? I found a related question…
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
1007
votes
65 answers

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?
Matt Howells
  • 40,310
  • 20
  • 83
  • 102
797
votes
36 answers

Convert int to binary string in Python

How do I convert an integer into a binary string in Python? 37 → '100101'
Nate
  • 18,892
  • 27
  • 70
  • 93
504
votes
24 answers

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 Wikipedia article and various other articles,…
452
votes
13 answers

Reading binary file and looping over each byte

In Python, how do I read in a binary file and loop over each byte of that file?
Jesse Vogt
  • 16,229
  • 16
  • 59
  • 72
431
votes
13 answers

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 is to ensure that the data remains intact without…
Lazer
  • 90,700
  • 113
  • 281
  • 364
424
votes
3 answers

How to convert 'binary string' to normal string in Python3?

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 string: >>> print(b'a string') b'a string' >>> print(str(b'a…
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
397
votes
8 answers

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 695 >>> 0100 64 How do you use literals to express binary in Python? Summary…
Justin Standard
  • 21,347
  • 22
  • 80
  • 89
380
votes
13 answers

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?
adam_0
  • 6,920
  • 6
  • 40
  • 52
305
votes
13 answers

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 are stored in memory using the two's complement…
Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
266
votes
15 answers

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 difference. I just need to know whether they are the…
sawa
  • 165,429
  • 45
  • 277
  • 381
261
votes
17 answers

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 as 00000110 instead of 110?
Smith
  • 2,793
  • 3
  • 16
  • 12
241
votes
24 answers

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 if there is a type in C++ for binary numbers, and…
hamza
  • 2,714
  • 3
  • 19
  • 14
235
votes
19 answers

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 me more intuitive) 10000001 which is binary 1…
Ray
  • 3,468
  • 8
  • 26
  • 27
232
votes
11 answers

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(1) -> 0b1 # What I would like: bin(1) ->…
Niels Sønderbæk
  • 3,487
  • 4
  • 30
  • 43
1
2 3
99 100