Visit Our UNIX and Linux User Community

Bitwise negation

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bitwise negation
# 1  
Old 05-15-2007
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...
# 2  
Old 05-16-2007
If the course says that ((~ 2#1001)) evaluates to 2#110, I wonder what shell they were using. With ksh I get:
$ typeset -i2 b
$ echo $((2#1001)) $((b=~2#1001)) $b
9 -10 -2#1010

With bash, there is no -i2 and I don't see an easy way to get binary output. I could code my own routine, but then the behavior would be whatever I decided. (But I get the "9 -10" part with bash.) Since ksh alone has a way to output binary, I did a few experiments with it. A negative number has many leading 1's while a non-negative has many leading zeros. ksh uses enough positions to output the the leftmost bit transistion. If you exceed the shell's integer size, you get undefined results, but clamping at positive or negative "infinity" (max/min integer) is common with both ksh and bash. The shells I checked have either 32 or 64 bit integers.
# 3  
Old 05-17-2007
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:

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.
# 4  
Old 05-17-2007
Originally Posted by dLloydm
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.
You are correct:
$ ((x = 2#1101 & 2#110))
$ ((y = ~x))
$ print - $y

Previous Thread | Next Thread
Test Your Knowledge in Computers #517
Difficulty: Medium
The most basic type of variable that can be used in a computer program is a numeric type.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Negation in Bash Globbing

$ ls -1 a.1 b.1 x_a.1 x_b.1 $ ls -1 * b.1 x_a.1 x_b.1 $ ls -1 ** a.1 b.1 x_a.1 x_b.1The last result is not as expected. Why? Thanks. (2 Replies)
Discussion started by: carloszhang
2 Replies

2. Shell Programming and Scripting

Bitwise comparison of cols

Hello, I want to compute the bitwise number of matches in pairwise fashion for all columns. The problem is I have 18486955 rows and 750 columns. Please help with code, I believe this will take a lot of time, is there a way of tracking progress? Input Org1 Org2 Org3 A A T A ... (9 Replies)
Discussion started by: ritakadm
9 Replies

3. Shell Programming and Scripting

how to use bitwise or operator in /bin/sh

please any one can suggest me how to use bitesie || opearator to do this #initallize a=0 b=0 #condition if then a=0 else a=1 fi #bitwise or opeartion b = a || b Please view this code tag video for how to use code tags when posting code and data. (3 Replies)
Discussion started by: Palaniappan
3 Replies

4. FAQ Submission Queue

Analysis in bitwise XOR

The purpose of this article is revealing the unrevealed parts of the bitwise XOR. As we aware, the truth table for the XOR operator is : A B A^B 0 0 0 0 1 1 1 0 1 1 1 0 For example , 1^2 will be calculated as given below: First the operands... (1 Reply)
Discussion started by: pandeesh
1 Replies

5. Homework & Coursework Questions


Hello, have anybody any idea, how to negate some expression? I have code that greps somethnig and I want filter from it some results...How to make it? IDontWantThisExpression=$1 grep 'some regex' | grep '$IDontWantThisExpression' testfile.txt Thanks for any advices. Faculty of... (2 Replies)
Discussion started by: Dworza
2 Replies

6. Shell Programming and Scripting

if statement negation

Hi all, I've been searching the internet and can't find an answer to this. Im trying to figure out how to negate a whole expression befor an if. I'll explain below. Let's say x=0 y=1 ] (this is false) I would like to negate this whole boolean condition. To negate the above... (4 Replies)
Discussion started by: rethymno19
4 Replies

7. Programming

bitwise and if

Hi Suppose we have these code lines: #define _IN_USE 0x001 /* set when process slot is in use */ #define _EXITING 0x002 /* set when exit is expected */ #define _REFRESHING 0x004 ... 1 main () { 2 3 unsigned r_flags =_REFRESHING; 4 5 if (r_flag &... (3 Replies)
Discussion started by: Puntino
3 Replies

8. Shell Programming and Scripting

negation in find path

Guys, Pl suggest me how to ignore a path in find command... I am aware of using "!" for other option... but how can i use it like this exampl... I want to search something for in / but not in Pl help. Thanks.. (2 Replies)
Discussion started by: clx
2 Replies

9. UNIX for Advanced & Expert Users

bitwise operators

can anybody write a program to divide a number by another number using bitwise operators (9 Replies)
Discussion started by: areef4u
9 Replies

Featured Tech Videos