Where to learn about “bit”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 14:31:25

问题


I am trying to find some books or resources talking about bit in detail so that for example I would be able to translate a number (like 16) into bits. I am currently a high school student and whenever reading a programming books I can understand almost everything except the bit/bitwise operators part. I just do not know how it works and why do people even invent bit & byte :(. Therefore, I hope that you guys can give me some resources suggestions talking about how to translate number/characters into bits.

Thank you for answering my question and have a great day!


回答1:


Try googling 'binary arithmetic.' Here's a pretty good article to get you started.




回答2:


Your problem is not related to coding or any particular programming language but more to maths, escpecially algebra and numeral systems. Then next stage will be processor x86 architecture basics, than you can go to a programming language.

BTW: Usually books about viruses for x86 are very good start to understand how CPU works but without understandign base-2 and base-16 systems you will not get much from them.




回答3:


http://en.wikiversity.org/wiki/Bitwise_operators

http://en.wikipedia.org/wiki/Binary_numbers

Once you learn how to represent numbers in base 2(binary or "bits"), bitwise operators are simple enough to understand.

I'll just give some simple example:

Let m=31532=111101100101100 and n=12325=11000000100101.

Then the result of n & m (bitwise AND - binary operator) is:

(if n(i) and m(i) are both 1, then the result is 1, 0 otherwise)

111101100101100
011000000100101
===============
011000000100100

the result of n | m (bitwise OR - binary operator) is:

(if n(i) and m(i) are 0, then the result is 0, 1 otherwise)

111101100101100
011000000100101
===============
111101100101101

the result of n ^ m (bitwise eXclusive OR - binary operator) is:

(if either n(i) and m(i) is 1, BUT NOT BOTH, then the result is 1, 0 otherwise)

111101100101100
011000000100101
===============
100101100001001

the result of ~n (bitwise NOT - unary operator) is:

(if n(i)=1 then result is 0, 1 otherwise)

111101100101100
===============
000010011010011

let k=3, the result of n >> k (bitwise right shift) is:

111101100101100
===============
000111101100101

They have simply shifted towards right k=3 times. This effectively divides the number by 2^k=8. It's commonly used as an optimization by the compiler.

conversely, let k=3, the result of n << k (bitwise left shift) is

111101100101100
===============
101100101100000

They have simply shifted towards left k=3 times. This effectively multiplies the number by 2^k=8. One thing to notice here is because 32bit integer can only hold upto 2^32-1, there has been an arithmetic overflow, that is the higher k bits have been cut off.

You just have to see these carefully and figure out these patterns.

They are important in programming because they are commonly used in setting/clearing the flag values.

since int32 has 32 binary digits, it can contain upto 32 different kinds of boolean flags(either 0 for false, or 1 for true)

I think this is about it. They are fairly intuitive.




回答4:


There is no difference between them as I don't feel they can be compared. A bit was "invented" because computers only know two states, on (1) or off (0). A byte is simply 8 bits. I don't know of any book that is dedicated entirely to the discussion of a bit or byte but if you look at books about logic design, digital fundamentals or any book on hardware architecture you will find more information on bits and how they are used. Assembly language also deals with bits much more than higher level langauges so you may want to look into that as well.




回答5:


Not quite a book, but the Wikipedia article on Binary representations of numbers goes into excessive detail. And there is a nice section on converting between bases that you might find useful.

There are some scary clever tricks to working with bitfields and binary representations of numbers; from the stb.h header:

int stb_bitcount(unsigned int a)
{
   a = (a & 0x55555555) + ((a >>  1) & 0x55555555); // max 2
   a = (a & 0x33333333) + ((a >>  2) & 0x33333333); // max 4
   a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
   a = (a + (a >> 8)); // max 16 per 8 bits
   a = (a + (a >> 16)); // max 32 per 8 bits
   return a & 0xff;
}

unsigned int stb_bitreverse8(unsigned char n)
{
   n = ((n & 0xAA) >> 1) + ((n & 0x55) << 1);
   n = ((n & 0xCC) >> 2) + ((n & 0x33) << 2);
   return (unsigned char) ((n >> 4) + (n << 4));
}

unsigned int stb_bitreverse(unsigned int n)
{
  n = ((n & 0xAAAAAAAA) >>  1) | ((n & 0x55555555) << 1);
  n = ((n & 0xCCCCCCCC) >>  2) | ((n & 0x33333333) << 2);
  n = ((n & 0xF0F0F0F0) >>  4) | ((n & 0x0F0F0F0F) << 4);
  n = ((n & 0xFF00FF00) >>  8) | ((n & 0x00FF00FF) << 8);
  return (n >> 16) | (n << 16);
}

Simply amazing. :)




回答6:


One of the best books I've ever read on binary math and bit shifting is Hacker's Delight. It's practically THE book on anything to do with superoptimization. I'd highly recommend reading it and if the material is too complex, working through it very slowly. I've had to rewrite standard library functions like strlen() for a hobby OS before and this book saved my life.




回答7:


To answer part of your question...

Just type 16 to binary in the Google search box. If you're feeling really brave, you can type 16 to hex to get your answer in hexadecimal. :)




回答8:


Everything in the world of Computer only can be represented by '0' & '1' string. Example, a int type general has 32 bits, long long type has 64 bits, n-bits type can represent number from 0 to 2^n-1.

Additionally, bit operations like '<<','>>','&',or '|' are faster than arithmetic operations in computer, because it use the hardware to do this. And many codes can be optimized according to this.

Transform a int to binary, first you should know is that any number can be represented by the combination of 2^0,2^1,2^2....2^k: like 6=2^2+2^1 , 13 = 2^3+2^2+2^0 and so on, then 6 and 13 can be write 0110 and 1101. In fact, this is a mathematic problem.

If you want to know more about the bit operations, I think you can search on google or wiki rather than here.




回答9:


I would suggest learning to program a z80 ,PIC or other microprocessor. Then you'll understand where bits and bytes come from. Before you do that though, you probably want to get dirty with digital electronics. The reason bits exist is because digital electronics only have signals with 2 values, namely on and off, hence 1 and 0, or a bit.



来源:https://stackoverflow.com/questions/6092146/where-to-learn-about-bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!