What absolute minimum should a programmer know about binary numbers and arithmetic?

Although I know the basic concepts of binary representation, I have never written code that uses binary arithmetic and operations.

I want to know

  • What are the basic concepts a programmer should know about binary numbers and arithmetic ?, and

  • In what "practical" ways can binary be used in programming. I saw some cool options for changing operators and XOR, etc., but there are some typical problems when using binary operations - the obvious choice.

Please indicate pointers to good reference material.

+3
source share
15 answers

, . , .

, , , , , . , , , , . .

:

  • (, , , ,...)
  • (, , , ,...)
  • (AND, OR, XOR, NOT, SHL, SHR, ROL, ROR,...)
  • (, , float, struct,...)

... Bit Twiddling Hacks, .

+6

-.

-, .

PHP , -:

error_reporting = E_ALL & ~E_NOTICE

, int :

isOdd = myInt & 1
+4
+3

- , , :

  • 2 2 ^ 16 , 2 ^ 32 4,3 . , , , 2147204921 -, - ", 2 ^ 31" - .

  • ; nybble .

  • - , .

  • , , - ( ORed , AND , , , XOR - , - .)

- , , , .

+3

-. , -, , , , .

+3

, , , .

javascript, " ", :

var check = true;
for(var i = 0; i < elements.length; i++)
   check &= elements[i].checked;
checkAll.checked = check;

.

Vec3f m_Corners[8]; 

void corners(float a_Size){ 
    for(size_t i = 0; i < 8; i++){ 
        m_Corners[i]  = a_Size * Vec3f(axis(i, Vec3f::X), axis(i, Vec3f::Y), axis(i, Vec3f::Z)); 
    } 
} 

float axis(size_t a_Corner, int a_Axis) const{ 
    return ((a_Corner >> a_Axis) & 1) == 1 
        ? -.5f 
        : +.5f; 
} 

for(int y = 0; y < 512; y++)
  for(int x = 0; x < 512; x++)
     if(x & y) pixels[x + y * w] = someColor;
     else pixels[x + y * w] = someOtherColor;

int next = 1 << ((int)(log(number) / log(2));

,

bool powerOfTwo = number & (number - 1);

, ( Sierpinksi) . , , .

+3

( ! -) , (: , ...; -).

+2

, : , - .

:

  • " " IEEE ( javascript JAVA C)
  • // .. (

. , , xor, , - . .

+2

, . , # Java, - - .

, C ++, , , - .

- protcol - ... , x86, . . , , . RFC, → http://tools.ietf.org/html/rfc4648, .

, , .

Billy3

+1

256 65536. , .

, . , , , .

, , - .

0

- , "2" , 10b 3.

0

(, C ), , , , . , .

, ( "", , ). :

  • .

  • , , .

  • , .

0

, c asm "" . , -.

, , x . , , 15 . URL . , , , FaxID PK (32 int), .

( 4 ) (32 int) . , base 64. , , .

0

.

| 2

| 16

| 10

| 8

.

.

112 8 = (1 x 8 ^ 2) + (2 x 8 ^ 1) + (4 x 8 ^ 0)

74 10 = (7 x 10 ^ 1) + (4 x 10 ^ 0)

, , XOR .. . , - , .

, , 11001111 base 2, .

AND:

P | Q | R
T | T | T
T | F | F
F | F | F
F | T | F

11001111 base 2 AND 00111111 base 2 = 00001111 base 2

.

0

Source: https://habr.com/ru/post/1711051/


All Articles