![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| bitwise and if | Puntino | High Level Programming | 3 | 04-28-2008 10:22 AM |
| negation in find path | anchal_khare | Shell Programming and Scripting | 2 | 01-16-2008 02:31 AM |
| resetting counter using bitwise XOR | mrgubbala | High Level Programming | 1 | 09-14-2006 01:58 AM |
| bitwise operators | areef4u | UNIX for Advanced & Expert Users | 9 | 08-04-2006 05:39 AM |
| Bit-fields and Bitwise operators | amatsaka | High Level Programming | 3 | 04-23-2002 05:13 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Bitwise negation
I am taking an online course on Unix scripting. The topic is Unix arithmetic operators and the lesson is Logical and bitwise operations. It is not clear how much storage space Unix uses to represent integers that are typed. Bitwise negation caused me to question how many bits are used to represent numbers.
The course gives an example: ((~ 2#1001)) evaluates to 2#110 This would imply that only 4 bits are used, otherwise I would expect the example to evaluate to 2#11110110 if 8 bits were used an so on. I do not have access to a Unix OS or this would be easy to check. I don't know if the shell matters, but I would assume BASH since it allows integers. So, how many bits are use to represent such values? I understand Unix represents everything as strings unless declared as integer such as using typeset -i. Is this the same using something like 2#1111? Thanks for any help... |
| Forum Sponsor | ||
|
|
|
|||
|
This has been very helpful. It reveals to me that UNIX is storing signed integer values just like most programming languages I am familiar with. The leading 1's for negative numbers would indicate that the highest order bit is a sign bit. Therefore 2#1010 is 10 and -2#1010 is -10 which would be represented as:
111....1110110 - depending on the bit size used. Therefore I am suspecting the course is not correct when it claims ((~ 2#1001)) evaluates to 2#110, but that it actually evaluates to: "-2#1010". If UNIX used unsigned integers in this case then it would evaluate to a very large number depending on the bit size used. Something like 2#111...11110110. From your analysis I do not suspect this is the case. There is one more thing I would like to verify. The course has the following question: What do you think is the output of the following piece of code? ((x = 2#1101 & 2#110)) ((y = ~x)) print - $y The answer given is 11, as in decimal eleven or 2#1011. From what I understand I think the answer should be -5 as in -2#101. I plan to inform the course provider of these possible issues but I would like to make sure there is an issue. Thanks for your help. |
|
||||
|
Quote:
$ ((x = 2#1101 & 2#110)) $ ((y = ~x)) $ print - $y -5 $ |
||||
| Google UNIX.COM |